我不断收到此错误。
我正尝试登录我的godaddy帐户,以便每天自动用新的文本更新我的网站。它一直说无法通过id找到元素,但是我已经在PyCharm编辑器中尝试了其他所有find_element_by_
方法。他们似乎都不起作用。 XPath,类,选择器,名称等。
"C:\Users\mperr\PycharmProjects\Selenium Scripts\venv\Scripts\python.exe" "C:/Users/mperr/PycharmProjects/Selenium Scripts/macAndCheeseBar.py"
Traceback (most recent call last):
File "C:/Users/mperr/PycharmProjects/Selenium Scripts/macAndCheeseBar.py", line 10, in <module>
user_input = driver.find_element_by_id("username")
File "C:\Users\mperr\PycharmProjects\Selenium Scripts\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\mperr\PycharmProjects\Selenium Scripts\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\mperr\PycharmProjects\Selenium Scripts\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\mperr\PycharmProjects\Selenium Scripts\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="username"]"}
(Session info: chrome=83.0.4103.61)
这是我的代码:
from selenium import webdriver
username = "myUsername"
password = "myPassword"
driver = webdriver.Chrome(r"C:\Users\mperr\Downloads\chromedriver_win32\chromedriver.exe")
driver.get("https://sso.godaddy.com/?realm=idp&path=%2Fproducts&app=account")
user_input = driver.find_element_by_id("username")
user_input.send_keys(username)
password_input = driver.find_element_by_id("password")
password_input.send_keys(password)
login_button = driver.find_element_by_id("submit Btn")
login_button.click()
这是html:
<input id="username" type="username" aria-labelledby="label-username" aria-required="false" class="username-input ctHidden form-control" value="">
<input id="password" type="password" aria-labelledby="label-password" aria-required="false" class="ctHidden form-control" value="">
<button target="_parent" tabindex="0" class="btn btn-primary btn-purchase btn-block" id="submitBtn" type="button">Sign In</button>
我在这里留言吗?
答案 0 :(得分:1)
您的代码没有等待页面完全加载。 我尝试对您的代码进行一些更改,这是我的代码,
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
username = 'your username'
password = 'your password'
driver =webdriver.Chrome(r"C:\Users\mperr\Downloads\chromedriver_win32\chromedriver.exe")
driver.get("https://sso.godaddy.com/")
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
) #wait for page to load, so element with ID 'username' is visible
user_input = driver.find_element_by_id("username")
user_input.send_keys(username)
password_input = driver.find_element_by_id("password")
password_input.send_keys(password)
login_button = driver.find_element_by_id("submitBtn")
login_button.click()
您可以阅读有关硒等待here的文档。 您甚至可以使用python的时间模块进行等待