【问题标题】:Can't interact with element in page using selenium, python and chromedriver [duplicate]无法使用 selenium、python 和 chromedriver 与页面中的元素交互 [重复]
【发布时间】:2023-04-10 08:26:01
【问题描述】:

我正在尝试使用 selenium、chromedriver 和 python 登录页面 (https://www.aprovaconcursos.com.br/questoes-de-concurso/cliente/login)。但是虽然元素很容易找到,但我想不出与之交互的方法。

我正在使用 python 3.7、chromedriver 和 windows 10 运行它。不要认为这可能是问题所在。似乎有一些元素覆盖了我试图插入数据的输入,但不知道是什么。

这是我用来执行此操作的代码。

**def login(username, senha):

    #entra no site e faz login

    driver.get('https://www.aprovaconcursos.com.br/questoes-de-concurso/cliente/login')
    time.sleep(30)
    site_username = driver.find_element_by_id("username")
    site_username.clear()
    site_username.send_keys(username)
    site_password = driver.find_element_by_id("password")
    site_password.clear()
    site_password.send_keys(senha)
    driver.find_element_by_class_name("btn btn-primary").click()**

问题如下:

Traceback (most recent call last):
   File "***.py", line 161, in <module>
        login(username,senha)
   File "***.py", line 125, in login
        site_username.clear()
   File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 95, in clear
        self._execute(Command.CLEAR_ELEMENT)
   File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
        return self._parent.execute(command, params)
   File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
        self.error_handler.check_response(response)
   File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
        raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable   (Session info: chrome=73.0.3683.103)   (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT
    10.0.17134 x86_64)

编辑

这个问题似乎与 EC 无关。尝试了提供的解决方案,但从未出现这种情况。

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    建议使用等待条件,而不是使用睡眠命令。在继续之前,您可以等待某个条件出现。这应该有助于避免元素不可见、不可点击等问题。

    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.visibility_of_element_located((By.ID, 'username')))
    

    您可以在这些链接中找到有关等待的更多帮助:

    https://selenium-python.readthedocs.io/waits.html

    https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

    【讨论】:

    • 感谢您的帮助。试了一下,代码按预期工作:webdriver 等待。但问题是它一直等到永远。我认为输入框上方有一些我没有看到的东西,这就是问题的原因。
    【解决方案2】:

    你应该添加while循环,例如:

    `

    int x = 0;
    while (x < 3) {
    try {
    WebElement we = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
    we.click();
    break;
    } catch (WebDriverException e) {
    }
    x++;
    }
    

    ` 添加这几行额外的代码将使单击更加健壮,脚本将尝试单击循环中的元素。

    【讨论】:

    • 谢谢。欧共体的问题是:它永远不会出现。我想我在构建这些输入框的方式上遗漏了一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 2020-10-05
    • 2020-04-05
    相关资源
    最近更新 更多