Python Selenium `move_by_offset` 不工作

标签 python selenium selenium-webdriver selenium-chromedriver

使用 Python Selenium 调用简单的滚动操作不起作用:

driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
time.sleep(2)
actions = ActionChains(driver)
actions.move_by_offset(500, 500).perform()

例如移动到元素的功能,有效 确定并执行滚动:

driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
time.sleep(2)

element = driver.find_element_by_css_selector(<Something>)
actions = ActionChains(driver)
actions.move_to_element(element).perform()

调用移动到具有偏移量的元素,再次不起作用:

driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
time.sleep(2)

element = driver.find_element_by_css_selector(<Something>)
actions = ActionChains(driver)
actions.move_to_element_with_offset(element, 500, 500).perform()

有什么原因吗?

最佳答案

move_by_offset 似乎无法滚动页面,但它仍然可以将鼠标移动到当前鼠标位置的偏移量。

为了确认我们可以尝试这样做:

driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
actions = ActionChains(driver)
actions.move_by_offset(300, 500).context_click().perform()

要按偏移量滚动页面,我们必须使用 js:

driver = webdriver.Chrome()
driver.get('https://www.wikipedia.org/')
driver.execute_script('window.scrollBy(0, 500)')  # x=0, y=500

关于Python Selenium `move_by_offset` 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52433411/

相关文章:

java - Selenium : find part of identifier

python - mypy 声明 IO[bytes] 与 BinaryIO 不兼容

python - 为什么我的 python 正则表达式不起作用?

python - `scikit-optimize` 包内的 TypeError

python - 如何像 Python numpy.fft.rfft 中那样在 cv::dft 中指定 FFT 长度

Java Selenium : Unable to locate element

java - 无法单击位于不同框架中的单选按钮

java - 当文件位于 Maven 项目的资源文件夹中时,使用 selenium WebDriver 打开文件

java - 无法在窗口之间切换 - 无法显示浏览器消息

ruby - watir-webdriver 无法在 opera 11.50、11.60 等中创建 "full-page"屏幕截图