我正在将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'
答案 0 :(得分: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')
答案 1 :(得分:0)
尝试使用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')