本文介绍了如何在Python中使用Selenium Webdriver滚动网页而不使用javascript方法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:

  • Window.scrollBy()
  • Window.scrollTo()

如果您的用例用于scroll()一个元素方法:


但是,如果要避免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(). 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:

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()

使用 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:

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

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

09-12 03:25
查看更多