ElementNotInteractableException:消息:无法使用带有Selenium WebDriver的GeckoDriver Firefox将元素滚动到视图中

时间:2020-01-08 04:46:15

标签: python-3.x selenium-webdriver xpath css-selectors webdriverwait

如何解决该错误:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <> could not be scrolled into view
通过Selenium使用Firefox时出现

错误?

该网站上的所有提示都没有帮助我。我尝试了所有可以找到的解决方案,包括通过WebDriverWait和JS。一种解决方案是:

selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (568, 1215) is out of bounds of viewport width (1283) and height (699)

我尝试调整浏览器窗口的大小,但是这也没有用。

我的代码:

webdriverDir = "/Users/Admin/Desktop/MyVersion/geckodriver.exe"
home_url = 'https://appleid.apple.com/account/'
browser = webdriver.Firefox(executable_path=webdriverDir)
browser.get(home_url)
browser.find_element_by_css_selector("captcha-input").click()

抛出窗口大小错误的解决方案:

actions = ActionChains(browser)
wait = WebDriverWait(browser, 10)
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "captcha-input")))
actions.move_to_element(element).perform()
element.click()

顺便说一句,这些相同的代码在Chrome中可以完美运行。但这很明显。

1 个答案:

答案 0 :(得分:1)

要将字符序列发送到<captcha-input>字段,您必须为element_to_be_clickable()引入 WebDriverWait ,并且可以使用以下任意一种Locator Strategies

  • 使用CSS_SELECTOR

    driver.get('https://appleid.apple.com/account#!&page=create')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.captcha-input input.generic-input-field"))).send_keys("JohnTit")
    
  • 使用XPATH

    driver.get('https://appleid.apple.com/account#!&page=create')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//captcha-input//input[@class='generic-input-field   form-textbox form-textbox-text  ']"))).send_keys("JohnTit")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:

AppleID_CaptchaInput