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

查看:419
本文介绍了如何在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(),则

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

  • Element.scrollIntoView()
  • 您可以在中找到详细的讨论.不同的滚动选项之间有什么区别?


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

    使用 expected_conditions -selenium.webdriver.support.wait"rel =" nofollow noreferrer> 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()
    

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

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