尝试使用Selenium webdriver登录时出错

时间:2015-05-10 14:38:56

标签: python selenium

我正在尝试登录Zoho Mail。但我得到一个错误,我无法弄清楚。我的代码如下所示:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://www.zoho.com/mail/login.html")
lid = driver.find_element_by_id("lid")
lid.send_keys("email@email.com")
pwd = driver.find_element_by_id("pwd")
pwd.send_keys("Password")
selenium.find_element_by_name("submit").click()


driver.close()

网站上的HTML元素:

用户名/电子邮件:

<input name="lid" id="lid" class="input usrbx" value="" onkeypress="clearmsg()" type="email">

密码:

<input name="pwd" id="pwd" class="input passbx" onkeypress="clearmsg()" type="password">

提交按钮

<button type="submit" id="submit_but" name="submit" class="submit_mobile">Sign In</button>

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Python_coding\Mail_bot.py", line 8, in <module>
    lid = driver.find_element_by_id("lid")
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 208, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 664, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 175, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"lid"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/PC/AppData/Local/Temp/tmpdgppxbxp/extensions/fxdriver@googlecode.com/components/driver-component.js:10271)
    at FirefoxDriver.prototype.findElement (file:///C:/Users/PC/AppData/Local/Temp/tmpdgppxbxp/extensions/fxdriver@googlecode.com/components/driver-component.js:10280)
    at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/PC/AppData/Local/Temp/tmpdgppxbxp/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
    at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/PC/AppData/Local/Temp/tmpdgppxbxp/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
    at DelayedCommand.prototype.execute/< (file:///C:/Users/PC/AppData/Local/Temp/tmpdgppxbxp/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)

1 个答案:

答案 0 :(得分:1)

您需要的元素位于iframe。要在iframe中使用元素,首先必须更改为该iframe。

将代码更改为:

...
driver = webdriver.Firefox()
driver.get("https://www.zoho.com/mail/login.html")
iframe = driver.find_element_by_id("zohoiam")
driver.switch_to_frame(iframe)
...