简体   繁体   English

Selenium - 找不到网站中 100% 存在的元素

[英]Selenium - Can't find elements that 100% exist in the website

I am doing a side project for fun to automate the website https://10fastfingers.com/ in order for Selenium to complete all achievements of the website automatically.我正在做一个有趣的辅助项目来自动化网站https://10fastfingers.com/以便 Selenium 自动完成网站的所有成就。 You can find my current code in my repository: https://github.com/jasperan/pyfastfingers您可以在我的存储库中找到我当前的代码: https://github.com/jasperan/pyfastfingers

However, I have encountered a problem with the login page:但是,我遇到了登录页面的问题:

https://10fastfingers.com/login https://10fastfingers.com/login

Selenium does not let me locate the following HTML tags, whose corresponding xpath values are presented below, right away: Selenium 不允许我找到以下 HTML 标记,其对应的 xpath 值如下所示:

//*[@id="UserEmail"]
[//*[@id="UserPassword"]

It seems like the website does not load them properly the first time, because even when I inspect these manually (with my own web client, Firefox, Chrome, or even Chromium but launched by myself...), I get automatically redirected to its grandparent:似乎网站第一次没有正确加载它们,因为即使我手动检查这些(使用我自己的 web 客户端、Firefox、Chrome,甚至是 Chromium,但由我自己启动......),我也会自动重定向到它的祖父母:

/html/body

When I've located this element manually, after a second inspection I can redirect to my desired email and password elements.当我手动定位此元素时,经过第二次检查后,我可以重定向到我想要的 email 和密码元素。

However, programatically, I can't do that.但是,以编程方式,我不能这样做。 No matter how many times I try to locate the element, it doesn't properly locate, throwing me the following exception every time:无论我尝试定位元素多少次,它都无法正确定位,每次都会抛出以下异常:

 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)

Here is some code that corresponds to my login function:这是一些与我的登录 function 对应的代码:

    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

You can find the complete code in my repository.您可以在我的存储库中找到完整的代码。

The issue is in问题在

driver.find_element_by_xpath('[//*[@id="UserPassword"]')

Remove first '[' in the xpath.删除 xpath 中的第一个“[”。

driver.find_element_by_xpath('//*[@id="UserPassword"]')

Have you tried the wait until function.你试过等到function。

wait = WebDriverWait(driver, 10)
men_menu = wait.until(ec.visibility_of_element_located((By.XPATH, "//*[@id="UserPassword"]")))

Your issue is xpath expression:您的问题是xpath表达式:

[//*[@id="UserPassword"]

It should be:它应该是:

//*[@id="UserPassword"]

But it seem like you can use .find_element_by_id instead .find_element_by_xpath , looks better.但似乎您可以使用.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()

Following import:导入后:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM