我已经使用Selenium一段时间了,正试图从其LinkedIn公司页面中提取LinkedIn公司ID。此部分中的每个标签都是动态的或不是唯一的。所以我去了xpath。
<code style="display: none" id="bpr-guid-3744658">
{"data":{"elements":["Ts9RZEZq7awx/7fUFM1C+A==,0"],"paging":{"count":10,"start":0,"links":[]}},"included":[{"$deletedFields":[],"width":800,"fileIdentifyingUrlPathSegment":"800_800/0?e=1536192000&v=beta&t=6ny2TVRT-FUOSV45OcAvzB5KWQlEJM0YmuPxexVNx0M","height":800,"$type":"com.linkedin.common.VectorArtifact","$id":"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage,artifacts,80a83d1c-6000-4576-ac86-2403150cd7ab-3"},{"$deletedFields":[],"width":400,"fileIdentifyingUrlPathSegment":"400_400/0?e=1536192000&v=beta&t=-5gPpNm7Ro7cttZe-oKhpFK0cwbm11VMH9uHoptg1Mk","height":400,"$type":"com.linkedin.common.VectorArtifact","$id":"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage,artifacts,80a83d1c-6000-4576-ac86-2403150cd7ab-2"},{"$deletedFields":[],"width":200,"fileIdentifyingUrlPathSegment":"200_200/0?e=1536192000&v=beta&t=4xw5nz_6JCFJpMCvqp3zYgpWu4vkjT88sgc7mmZBMiM","height":200,"$type":"com.linkedin.common.VectorArtifact","$id":"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage,artifacts,80a83d1c-6000-4576-ac86-2403150cd7ab-1"},{"$deletedFields":[],"width":100,"fileIdentifyingUrlPathSegment":"100_100/0?e=1536192000&v=beta&t=FrXLV0ITY8qMu6Qm-C6domrXpRbUWbbhWCZpeWffJfI","height":100,"$type":"com.linkedin.common.VectorArtifact","$id":"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage,artifacts,80a83d1c-6000-4576-ac86-2403150cd7ab-0"},{"$deletedFields":["attribution"],"artifacts":["urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage,artifacts,80a83d1c-6000-4576-ac86-2403150cd7ab-0","urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage,artifacts,80a83d1c-6000-4576-ac86-2403150cd7ab-1","urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage,artifacts,80a83d1c-6000-4576-ac86-2403150cd7ab-2","urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage,artifacts,80a83d1c-6000-4576-ac86-2403150cd7ab-3"],"rootUrl":"https://media.licdn.com/dms/image/C5603AQHWo0jcnWXI7A/profile-displayphoto-shrink_","$type":"com.linkedin.common.VectorImage","$id":"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage"},{"profilePicture":{"com.linkedin.common.VectorImage":"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo,profilePicture,com.linkedin.common.VectorImage"},"firstName":"Stephanie","lastName":"Goldberger","$deletedFields":[],"entityUrn":"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo","$type":"com.linkedin.voyager.identity.normalizedprofile.Profile"},{"inNetworkPeopleSearchUrl":"https://www.linkedin.com/vsearch/p?f_N=F&f_CC=1288","$deletedFields":[],"totalNumberOfConnections":1,"topConnectionsResolutionResults":{"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo":"urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo"},"topConnections":["urn:li:fs_normalized_profile:ACoAAAI8JnMBdj5agxf-k-p3EhA5XpUNvhknDIo"],"$type":"com.linkedin.voyager.jobs.shared.InNetworkRelevanceReasonDetails","$id":"Ts9RZEZq7awx/7fUFM1C+A==,0,details,com.linkedin.voyager.jobs.shared.InNetworkRelevanceReasonDetails"},{"$deletedFields":[],"company":"urn:li:fs_normalized_company:1288","details":{"com.linkedin.voyager.jobs.shared.InNetworkRelevanceReasonDetails":"Ts9RZEZq7awx/7fUFM1C+A==,0,details,com.linkedin.voyager.jobs.shared.InNetworkRelevanceReasonDetails"},"$type":"com.linkedin.voyager.organization.relevance.CompanyRelevanceReason","$id":"Ts9RZEZq7awx/7fUFM1C+A==,0"}]}
</code>
在inspect dev工具中,此命令返回预期结果:
e = $x('//*[contains(.,"bpr-guid-3744658")]')
但是,由于ID是动态的,因此我尝试了以下方法:
e = $x('//*[contains(.,"bpr-guid-")]')
这将返回结果,但是右边的节点不在列表中。我要查找的节点之后的节点实际上已进入列表。
我的程序是使用Python编写的,但是我正在使用JavaScript,因为它的调试速度更快(因此,使用Python解决方案倍受赞赏)。
答案 0 :(得分:0)
尝试使用starts_with
代替包含,我也建议使用id
代替.
,像这样:
CompanyList = driver.find_elements_by_xpath('.//code[starts-with(@id, "bpr-guid")]')
如果元素不一定总是带有标签<code
或它们没有start-with @id
= bpr-guid ,那么我们可以提出比您之前尝试缩小元素范围的另一个更直接的解决方案。
答案 1 :(得分:0)
//*[starts-with(@id, "bpr-guid")]