美文网首页
python selenium滑动到指定元素操作

python selenium滑动到指定元素操作

作者: 戒灵 | 来源:发表于2019-06-12 16:41 被阅读0次

假设我们要取的元素在页面中部需要滑动才能取到

driver.find_element_by_xpath("//span[contains(@class,'askSeeMoreQuestionsLink')]/span").click()
     time.sleep(2)
driver.find_element_by_xpath("//span[contains(@class,'askLoadMoreQuestionsLink')]/span").click()
     time.sleep(2)

显然单纯的上面写法是行不通的
下面我们使用js加载到指定id处,在找这两个元素,正确写法:
元素附近id:ask_lazy_load_div

 target = driver.find_element_by_id("ask_lazy_load_div")
 driver.execute_script("arguments[0].scrollIntoView();", target)
    time.sleep(5)
driver.find_element_by_xpath("//span[contains(@class,'askSeeMoreQuestionsLink')]/span").click()
    time.sleep(2)
driver.find_element_by_xpath("//span[contains(@class,'askLoadMoreQuestionsLink')]/span").click()
    time.sleep(2)

测试链接(登录过后的账号):https://www.amazon.com/dp/B01M0SFMIH

相关文章

网友评论

      本文标题:python selenium滑动到指定元素操作

      本文链接:https://www.haomeiwen.com/subject/trnlfctx.html