使用selenium处理python Bot,由于“arguments[0].scrollHeight”返回“None”,对话框中的无限滚动无法工作
dialogBx=driver.find_element_by_xpath("//div[@role='dialog']/div[2]")
print(dialogBx) #<selenium.webdriver.remote.webelement.WebElement (session="fcec89cc11fa5fa5eaf29a8efa9989f9", element="31bfd470-de78-XXXX-XXXX-ac1ffa6224c4")>
print(type(dialogBx)) #<class 'selenium.webdriver.remote.webelement.WebElement'>
sleep(5)
last_height=driver.execute_script("arguments[0].scrollHeight",dialogBx);
print("Height : ",last_height) #None
我需要最后的高度来比较,请建议解决方案
好的,为了回答你的问题,既然你在一个对话中,我们应该关注它。当您执行:
last_height=driver.execute_script("arguments[0].scrollHeight",dialogBx);
我相信您是在主页面或错误的div中执行的(不是100%确定)。无论我采用哪种方式,我们都将选择对话框中当前可用的最后一个<li>
项并向下滚动到其位置,这将强制更新对话框。我将从下面看到的完整代码中提取一个代码:我们首先选择最后一个列表项,然后将属性位置\u滚动到\u视图中。此属性将向下滚动对话框到最后一项,然后加载更多项。start_pos只是我们可用的
<li>
元素列表中的位置。即:<div><li></li><li></li><li></li></div>
start_pos=2,这是从0开始的最后一个li项。我之所以使用这个变量名,是因为它位于for循环中,for循环监视div中li项的变化,一旦看到完整代码,就会得到它另一方面,要执行此操作,只需更改顶部的参数并执行测试函数test()。如果您已经登录instagram,只需运行获取追随者列表()。 注意:使用此函数时,请使用此代码中的Follower类。如果愿意,可以删除,但需要修改函数。 重要:
当您执行此程序时,对话框项目将不断增加,直到没有更多的项目可加载,因此TODO将删除您已处理的元素,否则我相信当您开始点击大数字时,性能将变慢
如果你需要任何其他解释,请告诉我。现在代码是:
相关问题 更多 >
编程相关推荐