seleniumselenium-webdriverxpathqwebelement

How to identify webelement with dynamic id and name?


I can't define webelement as it has dynamic id and name. There are iframe in another iframe. Attributes id and name for second iframe are dynamic. I need to define the second iframe to switch on it http://prntscr.com/pqshpr Please help me with defining this dynamic elements.

 WebElement chartFrameFirst = driver.findElement(By.xpath("(.//iframe)[1]"));
    driver.switchTo().frame(chartFrameFirst);
    click(By.xpath(".//div[@id=\"tv_chart_container\"]"));
    WebElement chartFrameSecond = driver.findElement(By.xpath(".//iframe[@id=\"tradingview_1d329\"]"));
    driver.switchTo().frame(chartFrameSecond);

Solution

  • We can use contains() which is a method used in XPath expression as shown below

    WebElement chartFrameSecond = driver.findElement(By.xpath(".//iframe[contains(@id,\"tradingview_\")]"));