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")
1
  • what is the error message? Commented Jun 20, 2019 at 12:32

2 Answers 2

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
    
Sign up to request clarification or add additional context in comments.

3 Comments

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 Are you using Python's unit-test module?
For python-unittest See this, this and this discussion.
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")

4 Comments

thanks, but it doesn't work ... the test runs, but it doesn't even write a character.
I don't get any error, the whole test runs out, but it doesn't write me anything in the username field.
This because you need to provide some wait time WebDriverWait
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.