How to identify webelement with dynamic id and name?

89 Views Asked by At

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);
3

There are 3 best solutions below

0
On BEST ANSWER

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_\")]"));
0
On

You can find number of frames and then switch to

int noofframes=driver.findelements(By.tagName(“iframe”)).size();
driver.switchTo().frame(1);

Switch to frame using frame id:

new WebDriverWait(driver,20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt((By.tagName(“iframe”)));

driver.switchTo().frame(1);

Hope this help

0
On

We can user index to switch frame which is inside an iframe

driver.switchTo().frame(0).switchTo().frame(0);