2

I need advice on how to call send_keys for user input. If I assign a variable for the line self.browser.find_elements_by_id ("Login1_UserName") and then send it to send_keys, the solution does not work. What am I doing wrong?

 def login(Self):
     # login to the app
     username = self.browser.find_elements_by_id ("Login1_UserName")
     username.send_keys ("userone")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Tomasito
  • 306
  • 2
  • 15

2 Answers2

2

find_elements_* would return a List and you can't invoke send_keys() on a List. So you need to replace find_elements_* with find_element_* and you can use the following Locator Strategies:

def login(Self):
    # login to the app
    self.browser.find_element_by_id("Login1_UserName").send_keys("userone")

As per best practices, while invoking send_keys() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:

  • Using ID:

    WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.ID, "Login1_UserName"))).send_keys("Tomasito")
    
  • Using CSS_SELECTOR:

    WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#Login1_UserName"))).send_keys("Tomasito")
    
  • Using XPATH:

    WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='Login1_UserName']"))).send_keys("Tomasito")
    
  • 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
  • ok, Thank you. Its very usable. I have two features: `def testTitle(self): self.browser.get("http://localhost/test") self.assertEqual("Login",self.browser.title) WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.ID, "Login1_UserName"))).send_keys( "Tomasito")` `def Login(self): # login to app self.browser.find_element_by_id("Login1_UserName").send_keys("userone")` It only works if this line is in the function testTitle and not in my newly created Login function for separate logging. – Tomasito Jun 20 '19 at 12:56
  • @Tomasito Are you using Python's _unit-test_ module? – undetected Selenium Jun 20 '19 at 12:58
  • 1
    For [tag:python-unittest] See [this](https://stackoverflow.com/questions/49859370/python-webdriver-no-browser-launched-while-using-unittest-module/49860022#49860022), [this](https://stackoverflow.com/questions/49503046/what-is-unittest-in-selenium-python/49516313#49516313) and [this](https://stackoverflow.com/questions/52562186/how-to-define-test-methods-using-python-unittest/52563091#52563091) discussion. – undetected Selenium Jun 20 '19 at 13:22
0

This is because you have used find_elements_by_id("Login1_UserName") it will return list NOT the element.You should use find_element_by_id("Login1_UserName")

def login(Self):
     # login to the app
     username = self.browser.find_element_by_id("Login1_UserName")
     username.send_keys("userone")

Try this code see if this work.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver=webdriver.Chrome("path of chrome driver")
driver.get('url')
username=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'Login1_UserName')))
username.send_keys("userone")
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • thanks, but it doesn't work ... the test runs, but it doesn't even write a character. – Tomasito Jun 20 '19 at 12:41
  • I don't get any error, the whole test runs out, but it doesn't write me anything in the username field. – Tomasito Jun 20 '19 at 12:47
  • This because you need to provide some wait time `WebDriverWait ` – KunduK Jun 20 '19 at 12:48
  • is it possible that this is because the definition of self.browser.get I have in another function? Because if you move this command there, it will be done. – Tomasito Jun 20 '19 at 12:49