问题
I don't know why ActionChains move_to_element()
is not working with chromedriver >74.
(But it works on chromedriver 74 and geckodriver.)
Even though I add these three line before ActionChains, it still failed to move to element.
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()
And throw error as below:
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds (Session info: chrome=79.0.3945.117)
I also try to use move_to_element_with_offset mentioned in Selenium MoveTargetOutOfBoundsException even after scrolling to element, it still not working:
ActionChains(driver).move_to_element_with_offset(element, 5, 5).click().perform()
Below is my setting of chromedriver. Is there any settings impact to 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)
回答1:
As your use is to invoke click()
through ActionChains instead of presence_of_element_located()
and visibility_of_element_located()
you need to use the expected_conditions as element_to_be_clickable()
as follows:
Usage with ActionChains:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_css")))).click().perform()
If you have to
scrollIntoView()
before invokingclick()
you need to induce WebDriverWait for thevisibility_of_element_located()
and you can use the following Locator Strategy:WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xxxxx)) drvier.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()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Additional Considerations
Ensure that:
- Selenium is upgraded to current levels Version 3.141.59.
- ChromeDriver is updated to current ChromeDriver v79.0.3945.36 level.
- Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes)
- Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
- If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
- Take a System Reboot.
- Execute your
@Test
as non-root user. - Always invoke
driver.quit()
withintearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.
Update
As per your comments that:
options.add_experimental_option('w3c', False)
worked for you, but as per the Release Notes of ChromeDriver 75.0.3770.8:
Resolved issue 2536: Make standards mode (goog:chromeOptions.w3c:true) the default [Pri-2]
ChromeDriver 75.0
solves this issue.
So the bottom line is, chromeOptions.w3c
needs to be set as true
by default. It will be against the best practices to turn off w3c
in chromedriver to address the error. We have discussed this in length and breadth in the following discussions:
- How to turn off w3c in chromedriver to address the error unknown command: Cannot call non W3C standard command while in W3C
- Cannot call non W3C standard command while in W3C mode (Selenium::WebDriver::Error::UnknownCommandError) with Selenium ChromeDriver in Cucumber Ruby
回答2:
move_to_element
uses internally move_to
def move_to_element(self, to_element):
if self._driver.w3c: # default in chromedriver 79
self.w3c_actions.pointer_action.move_to(to_element)
def move_to(self, element, x=None, y=None):
#...
el_rect = element.rect
left_offset = el_rect['width'] / 2
top_offset = el_rect['height'] / 2
left = -left_offset + (x or 0)
top = -top_offset + (y or 0)
self.source.create_pointer_move(origin=element, x=int(left), y=int(top))
The mouse pointer is moved by offset based on the element position. You are locating the element and then scroll it into view using JavaScript, so the offset is calculated by the wrong coordinates.
Removing the JavaScript scroll should solve the problem.
来源:https://stackoverflow.com/questions/59819502/movetargetoutofboundsexception-problem-with-chromedriver-version-74