Python Selenium 无法在 linkedin.com 上找到元素,Chrome 开发者控制台找到它

Python Selenium cannot locate Element on linkedin.com, Chrome developer console finds it

所以,这是我的问题,每个拥有 Linkedin 帐户的人都可以帮助我: 我正在尝试 select 来自 Linkedin-Profilepages 的一些数据。 使用此 X 路径选择名称效果很好:

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 ")]')

位置和当前工作相同。

但随后就变得棘手了。我正在尝试 select 最后一个教育站,就像最后一个大学。 它在 chrome 开发人员控制台中 select 它工作正常,但 selenium 无法通过 "no such element" 错误找到它。在 selenium chrome 驱动程序的打开 window 中,我仍然能够通过查询找到元素。

我的查询是:

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 ")]')

我四处搜索,唯一找到的是关于 iFrame 的信息。据我所知,该元素未包含在 iFrame 中。但是最后有一个 js 脚本,这可能与它有关,因为我不太明白发生了什么:

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};

我真的不知道这是否与它有关,但可能有。我真的没思路了。

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

向下滚动来自这个答案:

缩放来自这个答案:

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

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

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