Home > Back-end >  Writing a string to a spesific field with find_element selenium
Writing a string to a spesific field with find_element selenium

Time:01-02

Hello I want to write my e-mail automatically to a field on a website. The code that I'm trying is

driver.find_element_by_variable("variable").send_keys(username)

Normally I can do what I want on different websites but for this website I guess HTML codes are written little bit different. Here is the HTML code.

<div ><input placeholder="[email protected]" matinput="" 

<input placeholder="[email protected]" matinput="" formcontrolname="username" type="text"
autocomplete="off" 

id="mat-input-0" data-placeholder="[email protected]" aria-invalid="false" aria-required="false">

How should I write the find_element code?

CodePudding user response:

driver.find_element_by_variable() isn't a valid Locator Strategy


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

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.mat-input-element[id^='mat-input'][placeholder='[email protected]'][data-placeholder='[email protected]']"))).send_keys("text")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@class, 'mat-input-element') and starts-with(@id, 'mat-input')][@placeholder='[email protected]' and @data-placeholder='[email protected]']"))).send_keys("berkay_doruk")
    
  • 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
    

CodePudding user response:

I can't be sure about the correct locator for that element since you didn't share a link to that page, however you can try the following:

  1. Get the web element.
  2. Click on it
  3. After the click send text to it

There are several possible locators that may work for this element. You can try this:

input = driver.find_element_xpath("//input[@formcontrolname='username']")
input.click()
input.send_keys(username)
  • Related