0

Using CSS selector in selenium python, I'm trying to log in to webpage automatically.

Instead of using:

driver.find_element_by_id()

I wanna practice using:

driver.find_elements_by_css_selector()

So i coded it like :

userId = driver.find_elements_by_css_selector('input[id = "pass"]')
userId.send_keys('blabla78945@gmail.com')

But it wont find the element that its supposed to be typed in. Whats the problem of this case?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
김은유
  • 17
  • 1
  • Hello, welcome on Stackoverflow! I don't think `input[id = "pass"]` is a valid thing, you can try using `input[type=password]` – popcorn Jan 20 '20 at 05:29
  • 1
    also you need to check that your code isn't running too fast because many times the page don't have time to load up , see browser.wait() until methods – Ευάγγελος Γρηγορόπουλος Jan 20 '20 at 05:58
  • first show real URL - different pages may need different solutions. – furas Jan 20 '20 at 06:07
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Jan 20 '20 at 06:14
  • you should use `find_element` without **s** to get single element. Now you use `find_elements` with **s** so get list of elements and you may need `[0]` to work with first element on list. Maybe first use `print( userId )` to see what you get. – furas Jan 20 '20 at 06:14

1 Answers1

0

You were close enough. If you were able to send a character sequence within the desired element using find_elements_by_id() as follows:

userId = driver.find_element_by_id('pass')
userId.send_keys('blabla78945@gmail.com')

Using as a Locator Strategy you can achieve the same as follows:

  • Using the id attribute only as a css_selector:

    userId = driver.find_element_by_css_selector("#pass")
    userId.send_keys('blabla78945@gmail.com')
    
  • Using the tagName <input> along with id attribute as a css_selector:

    userId = driver.find_element_by_css_selector("input#pass")
    userId.send_keys('blabla78945@gmail.com')       
    

Note: You need to use find_element_by_css_selector() which returns the desired webelement, where as find_elements_by_css_selector() returns a List and you can't invoke send_keys() on a List.


However, if you are trying to access the desired element right after the new page loads then ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using the id attribute as a CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#pass"))).send_keys("blabla78945@gmail.com")
    
  • Using the tagName <input> along with id attribute as a CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#pass"))).send_keys("blabla78945@gmail.com")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352