1

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 class="mat-form-field-infix ng-tns-c58-0"><input placeholder="jane.doe@email.com" matinput="" 

<input placeholder="jane.doe@email.com" matinput="" formcontrolname="username" type="text"
autocomplete="off" 
class="mat-input-element mat-form-field-autofill-control ng-tns-c58-0 ng-pristine ng-invalid cdk-text-field-autofill-monitored ng-touched"
id="mat-input-0" data-placeholder="jane.doe@email.com" aria-invalid="false" aria-required="false">

How should I write the find_element code?

Key_Keys
  • 35
  • 5

2 Answers2

0

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)
Prophet
  • 32,350
  • 22
  • 54
  • 79
0

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='jane.doe@email.com'][data-placeholder='jane.doe@email.com']"))).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='jane.doe@email.com' and @data-placeholder='jane.doe@email.com']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • and why did you write 20 ? What's the point of that? – Key_Keys Jan 01 '22 at 21:14
  • For the sake of demonstration I have set it as **20**, incase you are using a framework the wait time for presence, visibility and clickability would be defined in the configuration file. It shouln't wait more or less. – undetected Selenium Jan 01 '22 at 21:17