【问题标题】:Selenium Python ElementNotVisibleExceptionSelenium Python ElementNotVisibleException
【发布时间】:2018-08-02 12:57:47
【问题描述】:

我正在将 Selenium 与 Python 一起使用,并且我正在尝试填写登录表单,我已经 成功完成用户名字段,但无法完成密码。这是html:

<div class="loginNotVodafone" style="">

        <form id="loginNotVodafone" name="loginNotVodafone" action="https://urlblabla" method="post">      
          <!-- // GDPR select -->

          <fieldset>
          <input type="text" id="userFake2" name="userFake2" value="" autocomplete="off" placeholder="Username">
          <input type="hidden" id="user" name="UserName" class="hiddenTwo" value="">
          </fieldset>

          <fieldset>
          <input id="password" name="Password" value="" type="password" autocomplete="off" placeholder="Password">
          </fieldset>

        </form>
</div>

这是我的代码:

inputuser = driver.find_element_by_id("userFake2")
inputuser.send_keys('email@email.com')
sleep(1);

password = driver.find_element_by_xpath(".//fieldset[.//input[@id='password']]")
password.click()
sleep(1);
password.send_keys('password')

我总是收到错误:

selenium.common.exceptions.ElementNotVisibleException: Message: element not 
visible

追溯:

Traceback (most recent call last):
File "C:\Users\stefa\eclipse-workspace\main.py", line 36, in <module>
password.send_keys('password')
File "C:\Users\stefa\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\stefa\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Users\stefa\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\stefa\AppData\Local\Programs\Python\Python37\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 visible
(Session info: chrome=67.0.3396.99)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

编辑: 使用此命令,我可以让光标在密码字段中闪烁:

password = driver.find_element_by_xpath("//*@id='loginNotVodafone']/fieldset[2]").click()
password.send_keys('password')

但收到错误消息:

password.send_keys('password')
AttributeError: 'NoneType' object has no attribute 'send_keys'

【问题讨论】:

  • 你能发布完整的 TraceBack
  • 在帖子中添加

标签: python selenium login


【解决方案1】:

xpath 有问题,所以我使用了一个名为 ChroPath for Chrome 的插件,它给了我正确的绝对和相对路径

所以正确的命令是:

password = driver.find_element_by_xpath("/html[1]/body[1]/div[3]/div[1]/div[1]/div[1]/div[4]/form[1]/fieldset[3]/input[1]").send_keys('password')

或相对 Xpath:

password = driver.find_element_by_xpath("//form[@id='loginNotVodafone']//input[@id='password']").send_keys('password')

【讨论】:

    【解决方案2】:

    尝试使用WebDriverWait:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "Password")))
    

    注意:您必须添加一些导入:

    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.by import By
    

    WebDriverWait 将等待至少 10 秒,直到元素可以点击,然后才会点击它。整个代码会是这样的:

    inputuser = driver.find_element_by_id("userFake2")
    inputuser.send_keys('email@email.com')
    
    password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "Password")))
    password.click()
    sleep(1); # make sure, that you really need this pause
    password.send_keys('password')
    

    编辑:您也可以尝试使用JavaScript

    inputuser = driver.find_element_by_id("userFake2")
    inputuser.send_keys('email@email.com')
    
    password = driver.find_element_by_id("password")
    driver.execute_script("arguments[0].click();", password)
    sleep(1); # make sure, that you really need this pause
    password.send_keys('password')
    

    【讨论】:

    • 感谢您的帮助,但现在我收到“TimeoutException(message, screen, stacktrace)”,它仍然找不到密码字段
    • 这个字段在浏览器上可见吗?您确定此文件是您发布的 HTML 格式吗?
    • 我已在回答中添加信息,请查看
    • 我已经用其他一些信息更新了页面的代码(对不起,我不是这个领域的专家)。完整的 Traceback 在主帖中
    • 你试过driver.execute_script("arguments[0].click();", password)吗?
    猜你喜欢
    • 2016-10-29
    • 2015-10-02
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多