如何在Python中使用Selenium Webdriver滚动网页而不使用javascript方法execute_script() [英] 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()
,则窗口方法:
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:
- Window.scrollBy()
- Window.scrollTo()
如果您的用例用于
但是,如果要避免
However, if you want to avoid the 使用 move_to_element(). action_chains.html#module-selenium.webdriver.common.action_chains"rel =" nofollow noreferrer> 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:
使用 selenium.webdriver.support.expected_conditions .与视口. 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:
这篇关于如何在Python中使用Selenium Webdriver滚动网页而不使用javascript方法execute_script()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!scroll()
一个
execute_script()
与 WebElement 进行交互,则可以使用以下两(2)个其他选项:
execute_script()
to interact with a WebElement you have two (2) other options available as follows:
.此方法将自动
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()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Cart"))).click()