我正在尝试在此网站上自动登录,但它根本不会自动填充,这是我的代码。
我尝试在Google上搜索,Youtube,看到了非常类似的教程,按照每个教程,但每次出现错误或工作时都没有自动填充用户名和密码。
如果您能花一点时间查看以下代码并告诉我这是什么问题,为什么它不起作用我真的很感激。
任何帮助非常感谢
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest
class LoginTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome("C:/Program Files (x86)/Google/ChromeDriver/chromedriver.exe")
self.driver.get("ts2.travian.si/login.php")
def test_Login(self):
driver = self.driver
username = "FERIgeeks"
password = "test123"
usernameFieldName = "name"
passwordFieldName = "password"
loginButtonXpath = "//input[@value='Prijava']"
usernameFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_name(usernameFieldName))
passwordFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_name(passwordFieldName))
loginButtonElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
usernameFieldElement.clear()
usernameFieldElement.send_keys(username)
passwordFieldElement.clear()
passwordFieldElement.send_keys(password)
loginButtonElement.click()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
以下是我遇到的错误
Error
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in testPartExecutor
yield
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 601, in run
testMethod()
File "C:\Users\User\PycharmProjects\Home\travianBot\travianBot.py", line 22, in test_Login
loginButtonElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
File "C:\Users\User\PycharmProjects\Home\travianBot\travianBot.py", line 22, in <lambda>
loginButtonElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 309, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 787, in find_element
'value': value})['value']
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute
self.error_handler.check_response(response)
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
答案 0 :(得分:1)
变化:
self.driver.get("ts2.travian.si/login.php")
到
self.driver.get("http://ts2.travian.si/login.php")
并查找id =“s1”的按钮
loginButtonXpath = "s1"
loginButtonElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id(loginButtonXpath))