【问题标题】:How can I scroll to the location of a page where an element is visible with Selenium using Python?如何使用 Python 滚动到使用 Selenium 可见元素的页面位置?
【发布时间】:2018-03-25 15:40:06
【问题描述】:

我目前正在使用以下代码导航到页面中间,但它无法正常工作。

driver.execute_script("window.scrollTo(0, document.body.scrollHeight/2);")

另外,我正在尝试使用

element.location_once_scrolled_into_view

有人可以帮忙吗?

【问题讨论】:

    标签: python selenium scroll


    【解决方案1】:

    您可以在脚本中调用.scrollIntoView(),将元素作为参数传递:

    driver.execute_script("arguments[0].scrollIntoView();", element)
    

    还有move_to_element()内置硒动作:

    from selenium.webdriver.common.action_chains import ActionChains
    
    ActionChains(driver).move_to_element(element).perform()
    

    这里完美地突出了差异:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-01
      • 2010-11-27
      • 1970-01-01
      • 2016-07-18
      • 2023-03-07
      • 2015-04-12
      • 2020-07-19
      • 2017-06-30
      相关资源
      最近更新 更多