1

Currently I have a problem - I want to switch out of current frame to parent. Application have a lot of of inner frames, so using driver.switchTo().defaultContent() is not the case. In this discussion possible solution is suggested - you should remember all frames you visited and and iterate over them from the top using

driver.switchTo().defaultContent()

The main problem is in that fact, that in my application I, sometimes, forced to use switching to frames located by xpath and other locators - use switch by WebElement. And as it is already mentioned in discussion above after switching all located WebElement becomes invalid and can't be used for switching. So I should have some way to find this element inside frame again. Although i can locate index of this frame element executing some js calls(i.e (WebElement)executeScript("window.frames[i]").equals(myWebElement)), but it is not the case as index may change when some frames deleted from page. Either I can try to find frame names and id's but they may not be specified.

So I see the only possible solution - set some attribute to frames that parent of currently selected - for Example attribute active with value true. Also I should be able to do this for all methods that can switch frame (there exist 3). In all methods I can receive WebElement representing frame, but as far as I know to change attribute value you should execute some js code. So the problem is how to locate current WebElement in js.

Thanks for reading this all.

2 Answers 2

2

look at this code snippet above.that's the best way you can locate the current element in Js passing the element as an argumet to execute script and referring to it in js using arguments[0]

WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element); 
Sign up to request clarification or add additional context in comments.

Comments

0

you would have to implement a custom logic using javascript

if the element has id then you can use the findElementById() method available in javascript else use the findElementsByName() and then iterate over the array and match other properties to narrow down to your element

1 Comment

you could try webElement.setId("someValue") method to set an identifier for the the webelement and then in your javascript you can easily say elem = document.findElementById("someValue"); elem.Value ="somethingElse"; if the element does not have its own id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.