Thirumalai
Thirumalai

Reputation: 59

Unable to switch to the iFrame with same name but in different DIV tags of the same page

Based on the Tab selection two different views will be displayed. I am able to get the context of the Webelements in the first options, but unable to get the context for the remaining tab selection.

The problem is the iframe name is unique for the two selection but they are differntiate through DIV tabs.

Please find my html code and suggest a workaround to get the Webelement context in the DIV tag "folderProducer",

Note able to access the element "customerSearchForm:corpLastName" with default switch to statements.

enter image description here

Upvotes: 1

Views: 4012

Answers (3)

Naveen Chhaniwal
Naveen Chhaniwal

Reputation: 704

In this kind of case Xpath is a bast way to find an element. You can differentiate both element by their Xpath.

Upvotes: 0

Yi Zeng
Yi Zeng

Reputation: 32855

As you didn't post the Java code you wrote, so I can't point out what went wrong exactly.

However, I don't think same id matters anyway. (But that's really bad, you should change it though)

Remember, switchTo().frame() has three overloads, you shouldn't be using index or name/id, but pass in the frame element itself. See source code here.

WebDriver frame(int index); // unstable

WebDriver frame(String nameOrId); // not working in your case, as they identical

WebDriver frame(WebElement frameElement); // the one you want

Even driver.findElements(By.name("content")).get(N) is bad, as it depends on the order of elements, working but not elegant.

You can locate the frames either by parent <div> id attribute or by frame src attribute.

// switch out of all frames, just in case, you might not need this line here
driver.switchTo().defaultContent();

// switch to customer frame
WebElement customerFrame = driver.findElement(By.cssSelector("#folderCustomer iframe"));
// alternative css locator: By.cssSelector("iframe[src*='customerSearch']")
driver.switchTo().frame(customerFrame);

// now inside customer frame, you can do stuff

// when you done, switch out of it
driver.switchTo().defaultContent();

// switch to producer frame now
WebElement producerFrame = driver.findElement(By.cssSelector("#folderProducer iframe"));
// alternative css locator: By.cssSelector("iframe[src*='producerSearch']")
driver.switchTo().frame(producerFrame);

// now inside producer frame, you can do stuff

Upvotes: 3

Nathan Merrill
Nathan Merrill

Reputation: 8386

The problem is not that the elements have the same name, but rather that they are identical (even their parents have the same ID).

I would definitely bring this up with the developers of the site (the fact that two elements have the same ID...which is a big no-no on websites).

Regardless, you can do driver.findElements(By.name("content")).get(N) to get the Nth Iframe, and then switch to it.

Upvotes: 2

Related Questions