如何在不使用 javascript 方法 execute_script() 的情况下在 Python 中使用 selenium webdriver 滚动网页 [英] How to scroll a webpage using selenium webdriver in Python without using javascript method execute_script()

查看:27
本文介绍了如何在不使用 javascript 方法 execute_script() 的情况下在 Python 中使用 selenium webdriver 滚动网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用鼠标和滚动条滚动网页.我正在探索除

I am trying to scroll a web page by using mouse and scroll bar. I am exploring any other option than

"driver.execute_script("window.scrollBy(0, 5000'))"

我确实尝试了诸如 chrome 操作之类的选项,但似乎没有任何效果.如果有人知道如何解决这个问题,将需要一些指导.

I did try options like chrome actions, however nothing seems to be working. Would need some guidance if anyone has any idea how to solve this.

推荐答案

如果您的用例scroll()包含 DOM 文档的窗口,除了使用以下任一窗口方法:

If your usecase is to scroll() the window containing the DOM document, there is no better way other then using the either of the following Window Methods:

如果您的用例scroll() 一个 Element 没有比使用 元素方法:

If your usecase is to scroll() an Element there is no better way other then using the Element Method:

您可以在中找到详细讨论不同的滚动选项有什么区别?

<小时>

但是,如果您想避免 execute_script()WebElement 交互,您可以使用以下两 (2) 个其他选项:


However, if you want to avoid the execute_script() to interact with a WebElement you have two (2) other options available as follows:

  • Using move_to_element() from selenium.webdriver.common.action_chains. This method will automatically scroll the element within the Viewport.

  • 示例代码:

  • Example code:

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

使用 element_to_be_clickable() 来自 selenium.webdriver.support.expected_conditions.此 expected_conditionsselenium.webdriver.support.wait 将自动滚动视口.

Using element_to_be_clickable() from selenium.webdriver.support.expected_conditions. This expected_conditions when used in conjunction with selenium.webdriver.support.wait will automatically scroll the element within the Viewport.

  • 示例代码:

  • Example code:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Cart"))).click()

这篇关于如何在不使用 javascript 方法 execute_script() 的情况下在 Python 中使用 selenium webdriver 滚动网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆