【问题标题】:Selenium Python: Actions.move_to_element not workingSelenium Python:Actions.move_to_element 不起作用
【发布时间】:2021-03-25 09:00:49
【问题描述】:

我目前正在做一个 selenium 测试来测试一些分页按钮

这些按钮在屏幕视口之外...

在 Firefox 中我可以很好地执行这段代码 (显然与 driver = webdriver.Firefox)

from selenium.webdriver.common.action_chains import ActionChains
import selenium.common.exceptions
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
url = 'https://stackoverflow.com'

driver.get(url)
driver.maximize_window()

footer_link = driver.find_element_by_css_selector("#footer > div > nav > div:nth-child(1) > h5 > a")
action = ActionChains(driver)
action.move_to_element(footer_link).click().perform()
driver.quit()

(顺便说一句,这是我的错误的最小产生)

同样,代码在 Firefox 中运行良好

但是在 Chrome(89 版)中,代码失败并出现此错误

selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds

我已经尝试了一些方法来使其工作,并且我已经能够通过使用 execute_script() 滚动到元素来使其工作,但我需要使用 sleep()、隐式等待或显式等待。

我想使用既定的方式滚动到元素而不使用睡眠,也就是使用 ActionChains 对象

通过在互联网上的一点点挖掘,我认为这段代码可以工作,但我一直收到同样的错误......

任何帮助将不胜感激!

【问题讨论】:

  • 从错误中,你可以看到高度是 8545 Element is not clickable at point (137, 8545) 这就是为什么你不能滚动到它...试试this method

标签: python selenium selenium-chromedriver web-testing


【解决方案1】:

https://github.com/SeleniumHQ/selenium/issues/7315

您必须先将其滚动到视图中,

isvisible 也不检查元素是否在视口中,因此无法使用等待可见性

https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/8054

所以使用:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import selenium.common.exceptions
from selenium import webdriver
import time

from selenium.webdriver.support.wait import WebDriverWait

options = webdriver.ChromeOptions()

driver = webdriver.Chrome()
url = 'https://stackoverflow.com'

driver.get(url)
driver.maximize_window()

 
footer_link=WebDriverWait(driver, 10).until(EC.presence_of_element_located(
   (By.CSS_SELECTOR, "#footer > div > nav > div:nth-child(1) > h5 > a")))

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

time.sleep(3)


footer_link.click()

driver.quit()

您也可以使用自定义等待:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import selenium.common.exceptions
from selenium import webdriver
import time

from selenium.webdriver.support.wait import WebDriverWait

options = webdriver.ChromeOptions()

driver = webdriver.Chrome()
url = 'https://stackoverflow.com'

driver.get(url)
driver.maximize_window()

 
footer_link=WebDriverWait(driver, 10).until(EC.presence_of_element_located(
   (By.CSS_SELECTOR, "#footer > div > nav > div:nth-child(1) > h5 > a")))

driver.execute_script("$('footer').scrollIntoView(true)", footer_link)

WebDriverWait(driver, 100).until(lambda a: driver.execute_script(
    "return parseInt($('html').scrollTop())>6500"))


footer_link.click()

driver.quit()

这里我使用 jquery.scrollTop() 来获取当前滚动位置。

【讨论】:

  • 谢谢,这个答案很有道理,也是我一定会使用的方法,我只是希望 ActionChains 方法 move_to_element 能按预期工作....谢谢你的回答!!
猜你喜欢
  • 2016-02-14
  • 2016-04-29
  • 2012-04-22
  • 2015-09-27
  • 2017-06-14
  • 2018-12-26
  • 2019-01-04
  • 2011-02-20
  • 1970-01-01
相关资源
最近更新 更多