Pablo
Pablo

Reputation: 15

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

Upvotes: 0

Views: 83

Answers (3)

Amruta
Amruta

Reputation: 1166

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

Upvotes: 0

SeleniumUser
SeleniumUser

Reputation: 4177

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

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

Upvotes: 0

Amit Jain
Amit Jain

Reputation: 4587

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

Upvotes: 0

Related Questions