我正在做一个有趣的副项目,以使网站https://10fastfingers.com/自动化,以便Selenium自动完成网站的所有成就。您可以在存储库中找到我当前的代码:https://github.com/jasperan/pyfastfingers
但是,我的登录页面遇到了问题:
https://10fastfingers.com/login
Selenium让我无法立即找到以下HTML标记,其相应的xpath值如下所示:
//*[@id="UserEmail"]
[//*[@id="UserPassword"]
该网站似乎第一次无法正确加载它们,因为即使我手动检查了它们(使用自己的Web客户端,Firefox,Chrome甚至Chromium,但由我自己启动...),我也会自动获得重定向到其祖父母:
/html/body
当我手动找到此元素后,在第二次检查后,我可以重定向到所需的电子邮件和密码元素。
但是,以编程方式,我无法做到这一点。无论我尝试定位元素多少次,都无法正确定位,每次都会抛出以下异常:
File "pyfastfingers.py", line 112, in <module>
main()
File "pyfastfingers.py", line 100, in main
do_login(driver)
File "pyfastfingers.py", line 74, in do_login
password = driver.find_element_by_xpath('[//*[@id="UserPassword"]')
File "/home/j/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/j/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/home/j/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/j/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression [//*[@id="UserPassword"] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '[//*[@id="UserPassword"]' is not a valid XPath expression.
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.18.0-21-generic x86_64)
以下是与我的登录功能相对应的一些代码:
driver.get('https://10fastfingers.com/login')
placeholder = driver.find_element_by_xpath('/html/body')
email = driver.find_element_by_xpath('//*[@id="UserEmail"]')
password = driver.find_element_by_xpath('[//*[@id="UserPassword"]')
email.send_keys(os.environ['FINGERS_EMAIL'])
password.send_keys(os.environ['FINGERS_PASSWORD'])
login_button = driver.find_element_by_id('login-form-submit')
login_button.click()
# Login complete
您可以在我的存储库中找到完整的代码。
答案 0 :(得分:2)
问题出在
driver.find_element_by_xpath('[//*[@id="UserPassword"]')
删除xpath中的第一个'['。
driver.find_element_by_xpath('//*[@id="UserPassword"]')
答案 1 :(得分:1)
您是否尝试过等待功能。
wait = WebDriverWait(driver, 10)
men_menu = wait.until(ec.visibility_of_element_located((By.XPATH, "//*[@id="UserPassword"]")))
答案 2 :(得分:1)
您的问题是xpath
表达式:
[//*[@id="UserPassword"]
应为:
//*[@id="UserPassword"]
但是看起来您可以使用.find_element_by_id
代替.find_element_by_xpath
,看起来更好。
driver.get('https://10fastfingers.com/login')
email = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, 'UserEmail')))
email.send_keys(os.environ['FINGERS_EMAIL'])
password = driver.find_element_by_id('UserPassword')
login_button = driver.find_element_by_id('login-form-submit')
password.send_keys(os.environ['FINGERS_PASSWORD'])
login_button.click()
正在导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC