我正在使用以下代码运行python 3.9。当我运行脚本时,我会收到错误消息。不确定我错过了什么。该元素称为username。
File "/Users/user/Documents/PycharmProjects/webscrapping/app/webscraping.py", line 19, in <module>
login = driver.find_element("username") File "/Users/user/Documents/VENV/webscrapping/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, { File "/Users/user/Documents/VENV/webscrapping/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/Users/user/Documents/VENV/webscrapping/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator (Session info: chrome=98.0.4758.80) Stacktrace: 0 chromedriver 0x0000000102a5c3c9 chromedriver + 5018569 1 chromedriver 0x00000001029e7333 chromedriver + 4539187 2 chromedriver 0x00000001025bca88 chromedriver + 170632 3 chromedriver 0x00000001025f0d81 chromedriver + 384385 4 chromedriver 0x00000001025f14f1 chromedriver + 386289 5 chromedriver 0x00000001026238b4 chromedriver + 592052 6 chromedriver 0x000000010260e80d chromedriver + 505869 7 chromedriver 0x0000000102621604 chromedriver + 583172 8 chromedriver 0x000000010260e6d3 chromedriver + 505555 9 chromedriver 0x00000001025e495e chromedriver + 334174 10 chromedriver 0x00000001025e5935 chromedriver + 338229 11 chromedriver 0x0000000102a181ee chromedriver + 4739566 12 chromedriver 0x0000000102a31f51 chromedriver + 4845393 13 chromedriver 0x0000000102a37928 chromedriver + 4868392 14 chromedriver 0x0000000102a32a7a chromedriver + 4848250 15 chromedriver 0x0000000102a0cc31 chromedriver + 4693041 16 chromedriver 0x0000000102a4d978 chromedriver + 4958584 17 chromedriver 0x0000000102a4db01 chromedriver + 4958977 18 chromedriver 0x0000000102a63795 chromedriver + 5048213 19 libsystem_pthread.dylib 0x00007ff80f7a24f4 _pthread_start + 125 20 libsystem_pthread.dylib 0x00007ff80f79e00f thread_start + 15
代码试用:
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
CURRENT_DIR = os.getcwd()
print(f"Current Dir is {CURRENT_DIR}")
driver = webdriver.Chrome(f"{CURRENT_DIR}/chromedriver")
url = 'https://somewebsite.com/login2/'
driver.get(url)
time.sleep(2)
login = driver.find_element('username')
login.send_keys('myusername@somewebsite.com')
login.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit()
HTML正在尝试获取:
<div class="sc-dnqmqq jomEGJ" data-reactid=".0.1.0.2.1.0.0.0">
<label class="sc-iwsKbI hOobUj" for="username" data-reactid=".0.1.0.2.1.0.0.0.0">
<span data-reactid=".0.1.0.2.1.0.0.0.0.0">Username</span>
</label>
</div>
find_element((
find_element((查找一个给定By
策略的元素和一个定位器,其中两个参数都是必需的。
因此,您必须传递By
类,这是一组受支持的定位器策略以及定位器。
解决方案
实际上,你的代码行将是:
-
如果用户名是
class
属性的值:login = driver.find_element(By.CLASS_NAME, "username")
-
如果用户名是
id
属性的值:login = driver.find_element(By.ID, "username")
-
如果用户名是
name
属性的值:login = driver.find_element(By.NAME, "username")
-
如果用户名是
linktext
属性的值:login = driver.find_element(By.LINK_TEXT, "username")
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
username = "Username"
password = "Password"
time.sleep(5)
url = 'web_url'
browser.get(url)
browser.find_element(By.ID, "txtUserName").send_keys(username)
browser.find_element(By.ID, "txtPassword").send_keys(password)
time.sleep(5)
login_btn = browser.find_element(By.ID, "Button1")
result = login_btn.click()
note - @txtUserName - username btn id
@txtPassword - password btn id
@Button1 - login btn id
ref - https://iq.opengenus.org/python-script-to-open-webpage-login/