简体   繁体   中英

I am getting two errors while running my selenium code for classes and chrome gui errors

I want to write an automated script to login to twitter, but it was getting 2 errors... One with unable to find class that i mentioned and other was an chrome gui error.

from selenium import webdriver

driver = webdriver.Chrome(r"C:\Users\intel\Desktop\Downloads\abhay@\chromedriver_win32\chromedriver.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()


The errors i am getting are:

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:\Users\intel\Desktop\Python\test.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:\Users\intel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)

  File "C:\Users\intel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']

  File "C:\Users\intel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)

  File "C:\Users\intel\AppData\Local\Programs\Python\Python37-32\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":".js-username-field email-input js- 
    initial-focus"}
    (Session info: chrome=78.0.3904.87)

Selenium treats js-username-field email-input js-initial-focus as string with one class but it is string with three classes. So use js-username-field as class name or use other functions to search elements.

You can use css_selector with dots as start of every class (and without spaces)

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")

OR use xpath (with spaces)

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:\Users\intel\Desktop\Downloads\abhay@\chromedriver_win32\chromedriver.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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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