python - 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};
我真的不知道这是否与它有关,但它可能有。我严重没有想法。
解决方案
所以,我找到了这个解决方案,它可以帮助其他人试图从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
如果您滚动两个很远,它就不起作用,因为中间部分丢失了。所以我只是在给定的解决方案中添加了一个额外的步骤,首先只滚动页面的两半。
推荐阅读
- java - 有没有更简洁的方法来执行此 if 语句?
- c++ - 尝试在 Visual Studio 上用 C++ 创建桌面应用程序
- haskell - 我该如何表达这个约束?
- python - 在 OrderedDict 子类中覆盖没有 super() 的 __init__ 但父构造函数仍然有效
- r - 填写前一个单元格中的空白乘以R中不同列中的当前单元格
- javascript - 错误:发送后无法设置标头 node.js
- angular - 路由出错,提示没有路由提供者
- react-native - 如何防止在android和ios中选择屏幕中的屏幕截图反应本机
- javascript - 如何使用 ReactJS 动态呈现 html 块?
- java - 如果元素类型为,如何使用 Selenium(用于 Java)定位输入文本框