0

I have been trying to send a text to a text field that has the changing element id=PolarisTextField83 each time a log into a page (PolarisTextField## keeps changing its value like id=PolarisTextField45) as I have found that the id element is dynamic and the only static and unique part of the HTML is the placeholder example text which is placeholder="e.g. Shirts".

Therefore, I wonder if there is a way of locating placeholder="e.g. Shirts" then sending text to its respective type field (PolarisTextField##)?

I have tried to use

driver.findElementsByTagName("e.g. Shirts").sendKeys("test text");

but came to the understanding that I cannot follow .sendKeys() after findElementsByTagName.

I am new to both java and selenium and would appreciate any help and/or guidance!!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Kel Al
  • 3
  • 1

3 Answers3

1

You can find the element by placeholder value using XPath

driver.findElement(By.xpath("//*[@placeholder='e.g. Shirts']")).sendKeys("test text");
Med ben Amar
  • 51
  • 1
  • 4
0

Try below xpath to deal with dynamic web element.

WebDriverWait wait = new WebDriverWait(driver, 30);
driver.findElement(By.xpath("//*[starts-with(@id,'PolarisTextField')]")).sendKeys("Test");
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
0

To send a character sequence within the element you can use either of the following Locator Strategies:

  • Using css_selector, id and placeholder attributes:

    driver.findElement(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']")).sendKeys("test text");
    
  • Using xpath, id and placeholder attributes:

    driver.findElement(By.xpath("//*[starts-with(@id, 'Shirts') and contains(@placeholder, 'Shirts')]")).sendKeys("test text");
    

Ideally, to send a character sequence within the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("[id^='PolarisTextField'][placeholder$='Shirts']"))).sendKeys("test text");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[starts-with(@id, 'Shirts') and contains(@placeholder, 'Shirts')]"))).sendKeys("test text");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352