所以我正在学习如何使用Selenium进行Web自动化 - 我正在尝试编写一个脚本,将我的American Express余额返回到我的控制台。第一步实际上是成功登录...
看来我单击登录按钮的操作引发了一个错误,即找不到该元素,即使我在使用firebug时可以看到它。
这是我的代码:
from selenium import webdriver
driver = webdriver.Firefox()
baseurl = "https://www.americanexpress.com/canada/"
username = "myusername"
password = "mypassword"
xpaths = { 'usernameField' : "//input[@id='UserID']",
'passwordField' : "//input[@id='Password']",
'submitButton' : "//input[@id='loginButton']"
}
driver.get(baseurl)
driver.find_element_by_xpath(xpaths['usernameField']).clear()
driver.find_element_by_xpath(xpaths['usernameField']).send_keys(username)
driver.find_element_by_xpath(xpaths['passwordField']).clear()
driver.find_element_by_xpath(xpaths['passwordField']).send_keys(password)
driver.find_element_by_xpath(xpaths['submitButton']).click()
这是我收到的控制台错误消息,浏览器填写了我的登录详细信息,但是没有点击登录按钮:
Traceback (most recent call last):
File "get_balance.py", line 29, in <module>
driver.find_element_by_xpath(xpaths['submitButton']).click()
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 232, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 664, in find_element
{'using': by, 'value': value})['value']
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/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":"xpath","selector":"//input[@id='loginButton']"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/mx/dbnsd72j05zd4k2cls4cvmf00000gn/T/tmpKQDaey/extensions/fxdriver@googlecode.com/components/driver-component.js:10271)
at FirefoxDriver.prototype.findElement (file:///var/folders/mx/dbnsd72j05zd4k2cls4cvmf00000gn/T/tmpKQDaey/extensions/fxdriver@googlecode.com/components/driver-component.js:10280)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/mx/dbnsd72j05zd4k2cls4cvmf00000gn/T/tmpKQDaey/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/mx/dbnsd72j05zd4k2cls4cvmf00000gn/T/tmpKQDaey/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
at DelayedCommand.prototype.execute/< (file:///var/folders/mx/dbnsd72j05zd4k2cls4cvmf00000gn/T/tmpKQDaey/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)
有什么想法?非常感谢任何建议/帮助,谢谢!
答案 0 :(得分:2)
这是a
元素,而不是input
:
<a tabindex="0" href="#" id="loginButton" title="Login securely">
<span></span>
Log In
</a>
将您的xpath更改为://a[@id="loginButton"]
。
除此之外,对于id
属性,还有find_element_by_id()
方法:
driver.find_element_by_id("loginButton").click()
另外,如果你想让元素定位器与实际的“action”代码分开,你可以通过以下方式配置它(为了一个例子,只留下一个xpath表达式):
from selenium.webdriver.common.by import By
locators = {
'usernameField': (By.ID, "UserID"),
'passwordField': (By.XPATH, "//input[@id='Password']"),
'submitButton': (By.ID, "loginButton")
}
然后,您的“操作”代码将使用find_element()
:
username = driver.find_element(*locators['usernameField'])
username.clear()
username.send_keys(username)
password = driver.find_element(*locators['passwordField'])
password.clear()
password.send_keys(password)
login_button = driver.find_element(*locators['submitButton'])
login_button.click()