Am automating things using Selenium. Need your help to handle Dynamic Xpath as below:
Driver.findElement(By.xpath(\"//[@id=\'INQ_2985\']/div[2]/tr/td/div/div[3
Good to use Regular expression
driver.findElement(By.xpath("//*[contains(@id,'INQ_')]")
Note: If you have single ID with name starts from INQ_ then you can take action on the element . If a bunch of ID then you can extract as a List<WebElements>
and then match with the specific text of the element ( element.getText().trim() =="Linked Text"
and if it matched then take action. You can follow other logic to traverse and match.
you can use many methods, use implicity wait;
driver.findElement(By.xpath("//*[contains(@id,'select2-result-label-535')]").click();
driver.findElement(By.xpath("//*[contains(text(), 'select2-result-label-535')]").click();
you can use css -
div.context-menu-item-inner
Use this xpath:
driver.findElement(By.cssSelector("div.context-menu-item-inner").click();
The best choice is using full xpath instead of id which you can get easily via firebug.
e.g.
/html/body/div[3]/div[3]/div[2]/div/div[2]/div[1]/div/div[1]
if your xpath is varying
Ex: "//*[@id='msg500']" , "//*[@id='msg501']", "//*[@id='msg502']"
and so on...
Then use this code in script:
for (int i=0;i<=9;i++) {
String mpath= "//*[@id='msg50"+i+"']";
driver.findElement(By.xpath(mpath)).click();
}
you can try using contains()
or starts-with()
in xpath,
above xpath can be rewritten as follows,
Driver.findElement(By.xpath("//*[starts-with(@id,'INQ')]/div[2]/tr/td/div/div[3]/div")).click();
if you can post more of your html, we can help improve your xpath..
for example,if a "new table data or div" is added to the UI, above xpath will no longer be valid
cssSelectors
over xpath