首页 > 解决方案 > Python Selenium 无法在linkedin.com 上找到 Element,Chrome 开发者控制台可以找到它

问题描述

所以,这是我的问题,每个拥有 Linkedin 帐户的人都可以帮助我:我正在尝试从 Linkedin-Profilepages 中选择一些数据。使用此 X-Path 选择名称可以正常工作:

name = driver.find_element_by_xpath('//section[contains(concat(" ",normalize-space(@class)," ")," pv-top-card-v3 ")][contains(concat(" ",normalize-space(@class)," ")," artdeco-card ")][contains(concat(" ",normalize-space(@class)," ")," ember-view ")]//div/following-sibling::*[1]/self::div//div/following-sibling::*[1]/self::div//div[count(preceding-sibling::div)=0]//ul//li[count(preceding-sibling::li)=0][contains(concat(" ",normalize-space(@class)," ")," break-words ")]')

位置和当前工作相同。

但随后就变得棘手了。我正在尝试选择最后一个教育站,例如上一所大学。在 chrome 开发者控制台中选择它可以正常工作,但是 selenium 无法通过“没有这样的元素”错误找到它。在 selenium chromedriver 的打开窗口中,我仍然能够找到带有查询的元素。

我的查询是:

school = driver.find_element_by_xpath('//section[@id="education-section"]//ul//li[count(preceding-sibling::li)=0]//div//div//div//a//div/following-sibling::*[1]/self::div//div//h3[contains(concat(" ",normalize-space(@class)," ")," pv-entity__school-name ")]')

我用谷歌搜索,唯一发现的是 iFrames。据我所见,该元素未包含在 iFrame 中。但是最后有一个 js-script,这可能与它有关,因为我真的不明白发生了什么:

function(){var a=n.MessageChannel;"undefined"===typeof a&&"undefined"!==typeof window&&window.postMessage&&window.addEventListener&&!F("Presto")&&(a=function(){var a=window.document.createElement("IFRAME");a.style.display="none";a.src="";window.document.documentElement.appendChild(a);var b=a.contentWindow,a=b.document;a.open();a.write("");a.close();var c="callImmediate"+Math.random(),d="file:"==b.location.protocol?"*":b.location.protocol+"//"+b.location.host,a=(0,_.y)(function(a){if(("*"==d||a.origin==
d)&&a.data==c)this.port1.onmessage()},this);b.addEventListener("message",a,!1);this.port1={};this.port2={postMessage:function(){b.postMessage(c,d)}}});if("undefined"!==typeof a&&!F("Trident")&&!F("MSIE")){var b=new a,c={},d=c;b.port1.onmessage=function(){if(_.l(c.next)){c=c.next;var a=c.za;c.za=null;a()}};return function(a){d.next={za:a};d=d.next;b.port2.postMessage(0)}}return"undefined"!==typeof window.document&&"onreadystatechange"in window.document.createElement("SCRIPT")?function(a){var b=window.document.createElement("SCRIPT");
b.onreadystatechange=function(){b.onreadystatechange=null;b.parentNode.removeChild(b);b=null;a();a=null};

我真的不知道这是否与它有关,但它可能有。我严重没有想法。

标签: pythonseleniumlinkedin

解决方案


所以,我找到了这个解决方案,它可以帮助其他人试图从linkedin挖掘数据。由于 Linkedin 仅部分加载个人资料页面,问题是,该元素在开始时是不可见的。所以我用了两个步骤来实现,页面完全加载。首先,我缩小了,然后我向下滚动。

向下滚动来自这个答案: https ://stackoverflow.com/a/27760083/11192772

缩放来自这个答案: https ://stackoverflow.com/a/31482681/11192772

所以我在页面加载后添加了这个:

SCROLL_PAUSE_TIME = 1

# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
driver.execute_script("document.body.style.zoom='10%'")
while True:
    # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, (document.body.scrollHeight/2));")
    # Wait to load page
    sleep(SCROLL_PAUSE_TIME)
    driver.execute_script("window.scrollTo(0, (document.body.scrollHeight));")
    # Calculate new scroll height and compare with last scroll height
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

如果您滚动两个很远,它就不起作用,因为中间部分丢失了。所以我只是在给定的解决方案中添加了一个额外的步骤,首先只滚动页面的两半。


推荐阅读