Home > OS >  What is the correct way in Python Selenium to send text in text field?
What is the correct way in Python Selenium to send text in text field?

Time:12-02

Is it correct in Python selenium to send text in text field?

mobile= browser.find_element(By.name("mobile")).sendkeys("0000000000")
mobile.click()

CodePudding user response:

You are setting the variable mobile to be the return value of sendkeys(). Try this:

mobile= browser.find_element(By.name("mobile"))
mobile.sendkeys("0000000000")
mobile.click()

CodePudding user response:

Following the DeprecationWarning in ...

DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead

find_element_by_* commands are deprecated in the latest Selenium Python libraries and you have to use find_element() instead.


To send a character sequence to the text field you can use either of the following Locator Strategies:

You need to add the following import:

from selenium.webdriver.common.by import By
  • Using name:

    driver.find_element(By.NAME, "mobile").send_keys("0000000000")
    
  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "[name='mobile']").send_keys("0000000000")
    
  • Using xpath:

    driver.find_element(By.XPATH, "//*[@name='mobile']").send_keys("0000000000")
    

Ideally to send a character sequence to the text field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using NAME:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "mobile"))).send_keys("0000000000")
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='mobile']"))).send_keys("0000000000")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@name='mobile']"))).send_keys("0000000000")
    
  • 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
    
  • Related