Thomas Mitchell
Thomas Mitchell

Reputation: 1071

webdriver finding but not accessing elements

Is there a trick to selecting page elements one after the other using java webdriver? I am trying to set values in a form which is inside an iframe.

I first switch the iframe and can access and change the values in the first dropdown. I can create a WebElement from the other pages but I cannot interact with them at all.

The code below allows me to change the value of the first drop down.

driver.switchTo().frame(0)

WebElement fromList = driver.findElement(By.id("foo"));
r.selectItemByText(fromList, "var");

I cannot access any of the elements after that. I can find them all with no error but I cant do anything with them as it says they have no data.

WebElement fromList = driver.findElement(By.id("bar"));
r.selectItemByText(fromList, "foo");**

The second line returns this error:

NoSuchElementException: Cannot locate element with value 'foo'

If I remove it I get no error so the code is seeing the element just not the values of it.

Upvotes: 0

Views: 212

Answers (1)

Arran
Arran

Reputation: 25056

If the elements are on a different frame, you must switch back to the default frame, then back down again.

Something like:

driver.switchTo().defaultContent();

Reason is because once you switch to a frame, if you switch to another frame, the search will be only of the child frames to that original frame. So you must switch back to the top frame, and back down again.

Upvotes: 1

Related Questions