如何在不使用 javascript 方法 execute_script() 的情况下在 Python 中使用 selenium webdriver 滚动网页 [英] How to scroll a webpage using selenium webdriver in Python without using javascript method execute_script()
问题描述
我正在尝试使用鼠标和滚动条滚动网页.我正在探索除
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:
使用 move_to_element() 来自 selenium.webdriver.common.action_chains.此方法将自动滚动视口中的元素.
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_conditions 与 selenium.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屋!