Reputation: 65
Would you mind helping me to understand how I can handle this dynamic ID? Here are cases which I have already tried:
driver.findElement(By.xpath("//input[contains(@id,'Username')]")).sendKeys("aaa");
driver.findElement(By.xpath("//input[starts-with(@id,'undefined-undefined-Username-')]")).sendKeys("aaa");
driver.findElement(By.xpath("//*[@type='text']")).sendKeys("aaa");
No way to find that element.
Upvotes: 0
Views: 409
Reputation: 193108
As per the HTML you have shared the element is a dynamic element . To invoke click()
on the desired element you can use either of the following solutions:
cssSelector
:
driver.findElement(By.cssSelector("label[for^='undefined-undefined-Username-']")).sendKeys("aaa");
xpath
:
driver.findElement(By.xpath("//label[starts-with(@for,'undefined-undefined-Username-')][contains(.,'Username')]")).sendKeys("aaa");
As the element is dynamic you may need to induce WebDriverWait for the desired element to be clickable as follows:
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[for^='undefined-undefined-Username-']"))).sendKeys("aaa");
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[starts-with(@for,'undefined-undefined-Username-')][contains(.,'Username')]"))).sendKeys("aaa");
Upvotes: 1