MoveTargetOutOfBoundsException 问题与 chromedriver 版本 >74
问题:MoveTargetOutOfBoundsException 问题与 chromedriver 版本 >74 我不知道为什么 ActionChainsmove_to_element()不适用于 chromedriver >74。 (但它适用于 chromedriver 74 和 geckodriver。) 即使我在 ActionChains 之前添加了这三行,它仍然无法移动到元素。 WebD
问题:MoveTargetOutOfBoundsException 问题与 chromedriver 版本 >74
我不知道为什么 ActionChainsmove_to_element()
不适用于 chromedriver >74。
(但它适用于 chromedriver 74 和 geckodriver。)
即使我在 ActionChains 之前添加了这三行,它仍然无法移动到元素。
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, xxxxx)))
WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, xxxxx))
drvier.execute_script("arguments[0].scrollIntoView();", element)
ActionChains(driver).move_to_element(element).click().perform()
并抛出如下错误:
selenium.common.exceptions.MoveTargetOutOfBoundsException:消息:将目标移出边界(会话信息:chromeu003d79.0.3945.117)
即使在滚动到元素](https://stackoverflow.com/questions/56719535/selenium-movetargetoutofboundsexception-even-after-scrolling-to-element)之后,我也尝试使用[Selenium MoveTargetOutOfBoundsException 中提到的 move_to_element_with_offset ,它仍然无法正常工作:
ActionChains(driver).move_to_element_with_offset(element, 5, 5).click().perform()
下面是我的 chromedriver 设置。对 ActionChains 有任何设置影响吗?
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('log-level=3')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-proxy-server')
options.add_argument('--disable-extensions')
options.add_argument('--disable-infobars')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(executable_path=chromedriver_path, chrome_options=options)
解答
由于您的用途是通过 ActionChains 而不是presence_of_element_located()
和visibility_of_element_located()
调用click()
,因此您需要使用expected_conditionsas element_to_be_clickable()
如下:
- 与_ActionChains_一起使用:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_css")))).click().perform()
- 如果在调用
click()
之前必须到scrollIntoView()
,则需要为visibility_of_element_located()
诱导 WebDriverWait ,可以使用以下定位器策略:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xxxxx))
driver.execute_script("arguments[0].scrollIntoView();", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xxxxx)))
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_css")))).click().perform()
- 注意:您必须添加以下导入:
从 selenium.webdriver.support.ui 导入 WebDriverWait
从 selenium.webdriver.common.by 导入
从 selenium.webdriver.support 导入 expected_conditions 作为 EC
其他注意事项
确保这件事:
-
Selenium 升级到当前级别版本 3.141.59。
-
ChromeDriver 更新到当前ChromeDriver v79.0.3945.36级别。
-
Chrome 已更新至当前 Chrome 版本 79.0 级别。 (根据ChromeDriver v79.0 发行说明)
-
通过您的 IDE 和 Rebuild 仅使用所需的依赖项清理您的项目工作区。
-
如果您的基础_Web Client_版本太旧,请通过Revo Uninstaller卸载它并安装最新的GA和_Web Client_的发布版本。
-
进行_系统重启_。
-
以非root用户身份执行您的
@Test
。 -
始终在
tearDown(){}
方法中调用driver.quit()
以优雅地关闭和销毁 WebDriver 和 Web Client 实例。
更新
根据您的评论:
options.add_experimental_option('w3c', False)
为您工作,但根据ChromeDriver 75.0.3770.8的_Release Notes_:
已解决问题 2536:将标准模式 (goog:chromeOptions.w3c:true) 设为默认 [Pri-2]
ChromeDriver 75.0
解决了这个问题。
所以底线是,chromeOptions.w3c
需要默认设置为**true
**。在 chromedriver 中关闭w3c
以解决该错误将违反最佳实践。我们已经在以下讨论中详细讨论了这个问题:
-
如何在 chromedriver 中关闭 w3c 以解决错误未知命令:Cannot call non W3C standard command while in W3C
-
在 W3C 模式下无法调用非 W3C 标准命令 (Selenium::WebDriver::Error::UnknownCommandError) 在 Cucumber Ruby 中使用 Selenium ChromeDriver
更多推荐