我正在尝试使用Selenium自动连接到我的etoro帐户,并从我的投资组合中获取一些数据。我在谷歌实验室工作,从现在开始,这里是我的:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome(options=options)
wd.get("https://www.etoro.com/fr/login")
username = "username@mail.com"
password = "password"
elementID = wd.find_element_by_css_selector("input[automation-id='login-sts-username-input']")
elementID.send_keys(username)
elementID = wd.find_element_by_id("password")
elementID.send_keys(password)
但是,我有以下错误消息
Message: no such element: Unable to locate element: {"method":"css selector","selector":"input[automation-id='login-sts-username-input']"}
(Session info: headless chrome=91.0.4472.77)
我试图改变和使用find_element_by_class,by_xpath等,但我找不到该怎么做。
你能帮我一下吗?
如果不需要在无头模式下运行,则删除该参数可修复此问题。无论如何,这就是你的问题所在。
我怀疑在无头浏览器模式下,我们可能会遇到某种机器人检测。如果您需要无头运行,也许我们可以找到一种方法来解决这个问题。
看看这是否有效:-
driver.find_element_by_xpath(".//input[@id='username']")
为了完整起见,Selenium在版本4.3.0中删除了一些方法,因此您的代码应修改为:
...
elementID = wd.find_element("xpath",".//*[@id='username']")
...
elementID = wd.find_element("id","password")
...
请参阅属性错误:“WebDriver”对象没有属性“find_element_by_xpath”