我正在使用Selenium Webdriver登录网站。我尝试了多个不同的选择器,并尝试了隐式等待,但找不到元素。
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
url = "https://www.example.com"
login_page = browser.get(url)
username = browser.find_element_by_id("Email")
# Also tried:
# username = browser.find_element_by_xpath('//*[@id="Email"]')
# username = browser.find_element_by_css_selector('#Email')
username.send_keys("email")
这是html
<div class="form-group">
<label for="Email">Email address</label>
<div class="input-group" style="width: 100%">
<input class="form-control email" data-val="true" data-val-length="Maximum length is 50" data-val-length-max="50" data-val-regex="Provided email address is not valid" data-val-regex-pattern="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" data-val-required="Email is required" id="Email" name="Email" type="email" value=""><br>
<span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
</div>
以下是错误消息
Traceback (most recent call last):
File "seleniumloginpi.py", line 12, in <module>
email.send_keys('email')
File "/Users/greg/anaconda/envs/trade/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 320, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "/Users/greg/anaconda/envs/trade/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 461, in _execute
return self._parent.execute(command, params)
File "/Users/greg/anaconda/envs/trade/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/Users/greg/anaconda/envs/trade/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/fxdriver@googlecode.com/components/command-processor.js:10092)
at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/fxdriver@googlecode.com/components/command-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///var/folders/2h/3nnr94wx0f9g9bjcl0ks_g1w0000gn/T/tmpfAR5E7/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
非常感谢任何帮助。
答案 0 :(得分:1)
实际上你正在查找元素,send_keys
的问题,由于元素不可见,因此无法在电子邮件输入上设置值。但正如我在提供的HTML中看到的那样,电子邮件输入元素上不存在样式属性属性,这可能使其不可见。
可能有更多具有相同ID的元素,并且您正在与其他元素进行交互,您应该尝试使用一些不同的定位器,如下所示: -
username = browser.find_element_by_css_selector('div.input-group input#Email.form-control.email')
username.send_keys("email")
或尝试查找ID为Email
的所有元素,并对可见元素执行send_keys()
,如下所示:
usernames = browser.find_elements_by_id('Email')
for username in usernames:
if username.is_displayed():
username.send_keys("email")
break