简体   繁体   中英

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(eg 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();

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')]"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM