-1

I have this input field generated by JavaScript

<input class="postal-code form-text required form-control" autocomplete="postal-code" data-drupal-selector="edit-field-client-address-0-address-postal-code" type="text" id="edit-field-client-address-0-address-postal-code--bwzakXWpxR0" name="field_client_address[0][address][postal_code]" value="" size="10" maxlength="128" required="required" aria-required="true">

As you can see every time the id String is different: id="edit-field-client-address-0-address-postal-code--bwzakXWpxR0"

What is the proper way to locate this id using Selenium?

Ashok kumar Ganesan
  • 1,098
  • 5
  • 20
  • 48
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

4 Answers4

1

you can try using the following

driver.findElement(By.className("postal-code")).sendKeys("your value");
Arun Nair
  • 425
  • 3
  • 11
0

You can try with other locators if the id is dynamic.

CSS Selector:

driver.findElement(By.cssSelector(input[data-drupal-selector='edit-field-client-address-0-address-postal-code'])).sendKeys("your value");
Ashok kumar Ganesan
  • 1,098
  • 5
  • 20
  • 48
0

There are a LOT of attributes that seem to be unique. Have you tried any of them? Potential CSS selectors

input[autocomplete='postal-code']
input[data-drupal-selector='edit-field-client-address-0-address-postal-code']
input[name='field_client_address[0][address][postal_code]']

or you can try ID starts with, e.g.

input[id^='id="edit-field-client-address-0-address-postal-code--']
JeffC
  • 22,180
  • 5
  • 32
  • 55
0

As the desired element is an <input> element so moving next you would either invoke click() or sendKeys(), so to locate the element you need to use elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.postal-code[id*='address-postal-code'][name^='field_client_address']")));
    
  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='postal-code form-text required form-control' and contains(@id, 'address-postal-code')][starts-with(@name, 'field_client_address')]")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352