Handling Dynamic Xpath

后端 未结 6 527
心在旅途
心在旅途 2021-01-06 18:49

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

6条回答
  •  悲哀的现实
    2021-01-06 19:28

    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..

    • moreover using such long xpath's is not recommended, this may cause your test to fail more often

    for example,if a "new table data or div" is added to the UI, above xpath will no longer be valid

    • you should try and use id, class or other attributes to get closer to the element your trying to find
    • i personally recommend using cssSelectors over xpath

提交回复
热议问题