【问题标题】:How do I scroll down when there are multiple windows within a browser that has scroll bars using python selenium当浏览器中有多个窗口使用 python selenium 滚动条时,如何向下滚动
【发布时间】:2019-09-22 12:45:08
【问题描述】:

所以点击一个按钮后,弹出窗口(在图片中)弹出,它有 2 个带有滚动条的元素。我想向下滚动右侧的主要元素。

browser.execute_script("window.scrollBy(0, 10000)","")

这行代码不起作用。它没有抛出错误,但没有任何效果。

【问题讨论】:

  • 这些名称行是否在表中?如果不是所有这些行的父级是什么?如果我们知道父级和行html,我们可以根据行滚动
  • @supputuri 如何确定行是否在表中?我如何找出行的父母?
  • 如果不是,您必须在开发工具中单击检查并将移动移动到第一行上方,直到所有行都突出显示。这就是我们需要的父元素。
  • @supputuri 我已经包含了 HTML 的图片
  • 我数了28行

标签: python selenium selenium-webdriver selenium-chromedriver webautomation


【解决方案1】:

这里是在每一行上滚动和执行操作的逻辑。

driver.find_element_by_xpath("(//div[@clas='pagec_list_wrapper]/div[@class='pagelist_item])[last()]").location_once_scrolled_into_view
time.sleep(2)
driver.find_element_by_xpath("(//div[@clas='pagec_list_wrapper]/div[@class='pagelist_item])[last()]").location_once_scrolled_into_view

# get the number of rows
rows = driver.find_elements_by_xpath("//div[@clas='pagec_list_wrapper]/div[@class='pagelist_item]")
# now you can access each row here in for loop
for rowNumber in range(len(rows)):
    # scroll to the row
    driver.find_element_by_xpath("(//div[@clas='pagec_list_wrapper]/div[@class='pagelist_item])[" + str(rowNumber+1) "]").location_once_scrolled_into_view
    # if you want to click on the button (+) for that user
    driver.find_element_by_xpath("((//div[@clas='pagec_list_wrapper]/div[@class='pagelist_item])[" + str(rowNumber+1) "]//span[@class='ui_button_icon'])[1]").click()
    # you can use row element for any action with in the row
    row.xxxxxx

【讨论】:

  • 此代码返回一个包含 8 行的列表。因此,如果我 for 循环抛出返回的列表,那么我将只能点击 8 个按钮。但我想打更多。如何访问超过 8 行?这就是为什么我需要向下滚动
  • 更新了答案。
猜你喜欢
  • 2018-08-31
  • 2015-08-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
  • 2018-02-08
相关资源
最近更新 更多