selenium.common.exceptions.ElementNotInteractableException:消息:尝试使用Selenium和Python登录时元素不可交互错误

时间:2019-11-11 19:46:11

标签: python-3.x selenium selenium-webdriver selenium-chromedriver webdriverwait

我正在尝试使用python3中的Selenium登录https://www.ecobolsa.com/index.html,但是send_keys函数会向我发送消息:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: headless chrome=78.0.3904.97)

代码是:

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")

email = 'fake@gmail.com'
password = 'fakepass3'

driver = webdriver.Chrome('chromedriver', options=options)

driver.get('https://www.ecobolsa.com/index.html')

driver.find_element_by_id("userNameTextBox").send_keys(email)
driver.find_element_by_id("password_login").send_keys(password)

我尝试了其他解决方案,但没有一个成功。 我需要帮助。

2 个答案:

答案 0 :(得分:1)

要完成这项工作,您需要做很多事情。

  • 由于您正在处理无头模式,因此需要设置窗口大小。通过传递参数。 options.add_argument("window-size=1920x1080")

    -需要单击ACEPTO按钮,然后单击login链接。

    -诱导WebDriverWaitelement_to_be_clickable()

这是代码。

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("window-size=1920x1080")

email = 'fake@gmail.com'
password = 'fakepass3'

driver = webdriver.Chrome('chromedriver', options=options)
driver.get('https://www.ecobolsa.com/index.html')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[normalize-space()='ACEPTO']"))).click()
loginlink=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//li[@class='login']/a")))
ActionChains(driver).move_to_element(loginlink).click(loginlink).perform()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"userNameTextBox"))).send_keys(email)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"password_login"))).send_keys(password)
print('pass')

答案 1 :(得分:0)

要使用Selenium登录网站https://www.ecobolsa.com/index.html,您需要:

  • 为以下项引入 WebDriverWait

    • 带有文本ACEPTO的按钮可以点击。
    • 包含可单击的登录链接的元素。
    • 用户名字段可以单击。
  • 代码块:

    options = webdriver.ChromeOptions()
    options.add_argument("window-size=1920x1080")
    options.add_argument("--headless")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.ecobolsa.com/index.html")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.qc-cmp-button[onclick]"))).click()
    WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.qc-cmp-ui-container.qc-cmp-showing")))
    ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.login>a")))).click().perform()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userNameTextBox"))).send_keys("jatorna")
    driver.find_element_by_css_selector("input#password_login").send_keys("jatorna")
    driver.save_screenshot('./Screenshots/login.png')
    print("Program completed")
    driver.quit()
    
  • 浏览器快照:

login