我想写一个自动脚本来登录twitter,但它出现了2个错误。。。一个是找不到我提到的类,另一个是chromegui错误。
from selenium import webdriver
driver = webdriver.Chrome(r"C:UsersintelDesktopDownloadsabhay@chromedriver_win32chromedriver.exe")
driver.get("https://twitter.com/login")
login = driver.find_element_by_class_name("js-username-field email-input js-initial-focus").send_keys("hello")
passw = driver.find_element_by_class_name("js-password-field").send_keys("hello")
btn = driver.find_element_by_class_name("submit EdgeButton EdgeButton--primary EdgeButtom--medium").click()
我得到的错误是:
DevTools listening on ws://127.0.0.1:50766/devtools/browser/f3cecbe8-f7a2-431b-a8fe-67f5f52f2f62
[1105/173036.659:ERROR:command_buffer_proxy_impl.cc(124)] ContextResult::kTransientFailure: Failed to send GpuChannelMsg_CreateCommandBuffer.
Traceback (most recent call last):
File "C:UsersintelDesktopPythontest.py", line 7, in <module>
login = driver.find_element_by_class_name("js-username-field email-input js-initial-focus").send_keys("hello")
File "C:UsersintelAppDataLocalProgramsPythonPython37-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:UsersintelAppDataLocalProgramsPythonPython37-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 978, in find_element
'value': value})['value']
File "C:UsersintelAppDataLocalProgramsPythonPython37-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersintelAppDataLocalProgramsPythonPython37-32libsite-packagesseleniumwebdriverremoteerrorhandler.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":".js-username-field email-input js-
initial-focus"}
(Session info: chrome=78.0.3904.87)
Selenium将js-username-field email-input js-initial-focus
视为具有一个类的字符串,但它是具有三个类的串。因此,使用js-username-field
作为类名或使用其他函数来搜索元素。
您可以使用带点的css_selector
作为每个类的开始(并且没有空格(
driver.find_element_by_css_selector(".js-username-field.email-input.js-initial-focus")
driver.find_element_by_css_selector(".submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium")
或使用xpath
(带空格(
driver.find_element_by_xpath('//*[@class="js-username-field email-input js-initial-focus"]')
driver.find_element_by_xpath('//*[@class="submit EdgeButton EdgeButton--primary EdgeButtom--medium"]')
from selenium import webdriver
driver = webdriver.Chrome(r"C:UsersintelDesktopDownloadsabhay@chromedriver_win32chromedriver.exe")
driver.get("https://twitter.com/login")
login = driver.find_element_by_css_selector(".js-username-field.email-input.js-initial-focus").send_keys("hello")
#login = driver.find_element_by_xpath('//*[@class="js-username-field email-input js-initial-focus"]').send_keys("hello")
passw = driver.find_element_by_class_name("js-password-field").send_keys("hello")
btn = driver.find_element_by_css_selector(".submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium").click()
#btn = driver.find_element_by_xpath('//*[@class="submit EdgeButton EdgeButton--primary EdgeButtom--medium"]').click()