Maninder Singh
Maninder Singh

Reputation: 19

how i can handle dynamic "Id" in Selenium web driver?

How i can handle Dynamic "id" in Selenium Web driver, In my web form there is multiple session(e.g session start date and session end date) when i create a new session id of session is change how i can handle dynamic id in selenium webdriver?

Html code

<td id="_start_time_td_3" class="ui-widget-content">
    <input type="text" id="_start_time_3"
        name="start_time[]" maxlength="100" style="width: 120px;
        text-align: left;" class="starttime" required="required">
</td>

Selenium code

driver.findElement(By.id("_start_time_1")).click();
driver.findElement(By.xpath("//div[10]/div/div[2]/table/tbody/tr/td[4]/div")).click();
driver.findElement(By.id("_end_time_1")).click();
driver.findElement(By.xpath("//div[11]/div/div[2]/table/tbody/tr/td[5]/div")).click();

Upvotes: 0

Views: 821

Answers (1)

metar
metar

Reputation: 434

you can select the element by classname instead of id

driver.findElement(By.xpath("//input[@class='starttime']"));

or using contains

 driver.findElement(By.xpath("//input[contains(@id,'start_time')]"));

Upvotes: 1

Related Questions