我是python和selenium的新手,我正在尝试一个我在youtube中看到的例子。这是代码示例:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import unittest
class LoginTest(unittest.TestCase):
def test_Login(self):
self.driver = webdriver.Firefox()
self.driver.get("https://www.facebook.com/")
driver = self.driver
facebookUsername = "xxxxxxxx"
facebookPassword = "xxxxxxxx"
emailFieldId="email"
passFieldId ="pass"
loginButtonXpath="//input[@value='Log in']"
fbLogoXpath = "(//a[contains(@href,'logo')])[1]"
emailFieldElement = WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(emailFieldId))
passFieldElement = WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(passFieldId))
loginButtonElement = WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(loginButtonXpath))
emailFieldElement.clear()
emailFieldElement.send_keys(facebookUsername)
passFieldElement.clear()
passFieldElement.send_keys(facebookPassword)
loginButtonElement.click()
WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(fbLogoXpath))
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
当我运行代码时,它进入Facebook,但停止并给我这个错误。
ERROR: test_Login (__main__.LoginTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "facebot.py", line 25, in test_Login
loginButtonElement = WebDriverWait(driver, 1).until(lambda driver: driver.find_element_by_id(loginButtonXpath))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 76, in until
raise TimeoutException(message, screen, stacktrace)
TimeoutException: Message:
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdriv
er@googlecode.com/components/driver-component.js:10667)
at FirefoxDriver.prototype.findElement (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdriver@google
code.com/components/driver-component.js:10676)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdrive
r@googlecode.com/components/command-processor.js:12643)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdriver@
googlecode.com/components/command-processor.js:12648)
at DelayedCommand.prototype.execute/< (file:///c:/users/ale/appdata/local/temp/tmpmle1b1/extensions/fxdriver@googlec
ode.com/components/command-processor.js:12590)
----------------------------------------------------------------------
Ran 1 test in 16.262s
我试过通过查看youtube来修复它,但我卡住了,不知道它会是什么。
答案 0 :(得分:3)
首先,超时值太小,增加它。另外,您只能定义WebDriverWait
实例一次并重复使用:
wait = WebDriverWait(driver, 10)
emailFieldElement = wait.until(lambda driver: driver.find_element_by_id(emailFieldId))
passFieldElement = wait.until(lambda driver: driver.find_element_by_id(passFieldId))
loginButtonElement = wait.until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
另请注意,在找到“登录”按钮时,您应使用find_element_by_xpath()
代替find_element_by_id()
。