Handle elements that have changing ids all the time through Selenium Webdriver

前端 未结 3 958
小鲜肉
小鲜肉 2020-12-20 07:36

I am running the script to automate test cases and having this unique problem. I have detected and used IDs of the elements for click etc. purpose. However, all of a sudden

相关标签:
3条回答
  • 2020-12-20 07:59

    You can utilize CSS for this. For your element, looks like its:

    <* id="tile276_input" />
    

    What you need to do is find out what is changing about it. I assume it's the number inbetween. If it is, then your selector would look something like:

    By.cssSelector("*[id^='tile'][id$='input']")
    

    This will look for anything that has an ID that "starts with tile" and "ends with input. In our case, "tile276_input" matches that.

    See this article if you want more information

    0 讨论(0)
  • 2020-12-20 08:00
    WebElement element = driver.getElement(By.cssSelector("[id^='title']);
    

    Or

    WebElement element = driver.getElement(By.cssSelector("id:contains('title')"));
    

    You Can use this element to do desired actions.

    0 讨论(0)
  • 2020-12-20 08:03

    You also can try contains and starts-with() for such things

    driver.findElement(By.xpath("//*[contains(@id,'title')]"))
    

    or

    driver.findElement(By.xpath("//* [start-with(@id,'title')]"))
    
    0 讨论(0)
提交回复
热议问题