<div>
<iframe id="cq-cf-frame ">
<iframe id="gen367">
<body spellcheck="false" id="CQrte" style="height: 255px; font-size: 12px; font-family:tahoma,arial,helvetica,sans-serif; background-image: url("/libs/cq/ui/widgets/themes/default/ext/form/text-bg.gif"); background-repeat: repeat-x; background-attachment: fixed;">
<p>4t43t4<br></p>
</body >
</iframe>
</iframe>
</div>
In this scenario there is an iframe under iframe. And I have to select the outer iframe to go to inner iframe and write in the body which is in the inner iframe.
Next, I have to come out from the inner iframe to outer iframe and click on OK button, (which is in the outer iframe).
Following is my code
/*Line 1 */ driver.switchTo().frame("cq-cf-frame");
/* 2 */ driver.findElement(By.css("#extdd-9 > div.tblRow > input.edititem").click();
/* 3 */ driver.switchTo().Frame("cq-gen379");
/* 4 */ driver.findElement(By.id("CQrte").sendKeys("Tnx");
/* 5 */ selenium.selectFrame("relative=up");
/* 6 */ driver.findElement(By.xpath("//button[text()='OK']")).click();
Following is my problem:
My test code is working fine up to line number 4 i.e. writing into the body, but I want to come out from inner to outer iframe it says that the element //button[text()='OK'] not found.
I tried with using index, parent, relative, but had no luck.
NOTE: If I don’t select the inner frame (cq-gen379). I'm able to click on the OK button.
In Webdriver, you should use driver.switchTo().defaultContent(); to get out of a frame.
You need to get out of all the frames first, then switch into outer frame again.
// between step 4 and step 5
// remove selenium.selectFrame("relative=up");
driver.switchTo().defaultContent(); // you are now outside both frames
driver.switchTo().frame("cq-cf-frame");
// now continue step 6
driver.findElement(By.xpath("//button[text()='OK']")).click();
You have to get back out of the Iframe with the following code:
driver.switchTo().frame(driver.findElement(By.id("frameId")));
//do your stuff
driver.switchTo().defaultContent();
You need to first find iframe. You can do so using following statement.
WebElement iFrame= driver.findElement(By.tagName("iframe"));
Then, you can swith to it using switchTo method on you WebDriver object.
driver.switchTo().frame(iFrame);
And to move back to the parent frame, you can either use switchTo().parentFrame() or if you want to get back to the main (or most parent) frame, you can use switchTo().defaultContent();.
driver.switchTo().parentFrame(); // to move back to parent frame
driver.switchTo().defaultContent(); // to move back to most parent or main frame
Hope it helps.
To get back to the parent frame, use:
driver.switchTo().parentFrame();
To get back to the first/main frame, use:
driver.switchTo().defaultContent();
WebDriver driver=new FirefoxDriver();
driver.get("http://www.java-examples.com/java-string-examples");
Thread.sleep(3000);
//Switch to nested frame
driver.switchTo().frame("aswift_2").switchTo().frame("google_ads_frame3");
Below approach of frame handling : When no id or name is given incase of nested frame
WebElement element =driver.findElement(By.xpath(".//*[#id='block-block19']//iframe"));
driver.switchTo().frame(element);
driver.findElement(By.xpath(".//[#id='carousel']/li/div/div[3]/a")).click();
Selenium Web Driver Handling Frames
It is impossible to click iframe directly through XPath since it is an iframe. First we have to switch to the frame and then we can click using xpath.
driver.switchTo().frame() has multiple overloads.
driver.switchTo().frame(name_or_id)
Here your iframe doesn't have id or name, so not for you.
driver.switchTo().frame(index)
This is the last option to choose, because using index is not stable enough as you could imagine. If this is your only iframe in the page, try driver.switchTo().frame(0)
driver.switchTo().frame(iframe_element)
The most common one. You locate your iframe like other elements, then pass it into the method.
driver.switchTo().defaultContent(); [parentFrame, defaultContent, frame]
// Based on index position:
int frameIndex = 0;
List<WebElement> listFrames = driver.findElements(By.tagName("iframe"));
System.out.println("list frames "+listFrames.size());
driver.switchTo().frame(listFrames.get( frameIndex ));
// XPath|CssPath Element:
WebElement frameCSSPath = driver.findElement(By.cssSelector("iframe[title='Fill Quote']"));
WebElement frameXPath = driver.findElement(By.xpath(".//iframe[1]"));
WebElement frameTag = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame( frameCSSPath ); // frameXPath, frameTag
driver.switchTo().frame("relative=up"); // focus to parent frame.
driver.switchTo().defaultContent(); // move to the most parent or main frame
// For alert's
Alert alert = driver.switchTo().alert(); // Switch to alert pop-up
alert.accept();
alert.dismiss();
XML Test:
<html>
<IFame id='1'>... parentFrame() « context remains unchanged. <IFame1>
|
-> <IFrame id='2'>... parentFrame() « Change focus to the parent context. <IFame1>
</html>
</html>
<frameset cols="50%,50%">
<Fame id='11'>... defaultContent() « driver focus to top window/first frame. <html>
|
-> <Frame id='22'>... defaultContent() « driver focus to top window/first frame. <Fame11>
frame("relative=up") « focus to parent frame. <Fame11>
</frameset>
</html>
Conversion of RC to Web-Driver Java commands. link.
<frame> is an HTML element which defines a particular area in which another HTML document can be displayed. A frame should be used within a <frameset>. « Deprecated. Not for use in new websites.
Related
I'm trying to use selenium to type in data in inputboxes. But I cant get any element (NoSuchElementException). Problem is only with this site.
I tried searching by name/id but it failed.
WebDriver driver = new FirefoxDriver();
driver.get("https://ekrs.ms.gov.pl/web/wyszukiwarka-krs/strona-glowna");
System.out.println(driver.getCurrentUrl());
System.out.println("Successfully opened the website");
WebElement wb = driver.findElement(By.id("rejestrPrzedsiebiorcy"));
My goal (for now) is just to get this element :P.
The problem is the form is included in the source via <iframe> element. You can see that it has attribute src="https://ekrs.ms.gov.pl/krsrdf/krs/wyszukiwaniepodmiotu?". If you go to that link, you will see standalone form. The question is - how to access an included source? It's quite simple ;)
Find the <iframe> element:
WebElement frame = driver.findElement(By.xpath("//div[#class='portlet-body']/div/iframe"))
Switch to that frame:
driver.switchTo().frame(frame)
And that's it! Now you are in the <iframe> element context, and you can search inside it. so this will work now:
WebElement wb = driver.findElement(By.id("rejestrPrzedsiebiorcy"));
To switch back (get out of the frame context) you just have to call:
driver.switchTo().defaultContent();
Note that this site has dynamically generated ids to prevent automation, and uses CaptchaV3 (you can see I used xpath expression to find the iframe). Selenium is easily detectable if you are not careful.
Carefully observe the HTML code after inspecting required element. If you element is inside <iframe> then you need to switch on to frame first and then find that element.
below is the way to switch to frame:
driver.switchTo().frame() method takes one of the three possible arguments:
A number.
Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index 0, the second at index 1 and the third at index 2. Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.
driver.switchTo().frame(0)
A name or ID.
Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
driver.switchTo().frame("name here");
A previously found WebElement.
Select a frame using its previously located WebElement.
WebElement iframeElement = driver.findElement(By.id("IF1"));
//now use the switch command
driver.switchTo().frame(iframeElement);
Try driver.findElement(By.id("form-main")); first, then driver.findElement(By.id("rejestrPrzedsiebiorcy")); again.
When I am trying to execute below code its giving me exception as:-org.openqa.selenium.ElementNotVisibleException: element not visible
WebElement elem = newDriver.findElement(By.name("loginId"));
elem.get(0).clear();
elem.get(0).sendKeys("asd");
even though element is present.
For more details see the below image.
I am trying to access input box below account label but its giving me exception as element is not visible.
I already used Actions tag and JavascriptExecutor
Any suggestions.
The interested element is inside an iframe:
So, before, you have to switch to the iframe:
WebElement iframe= driver.findElement(By.id("alibaba-login-box"));
driver.switchTo().frame(iframe);
If you want to go "out" the iframe:
driver.switchTo().defaultContent();
The entire code:
WebElement iframe= driver.findElement(By.id("alibaba-login-box"));
driver.switchTo().frame(iframe);
WebElement elem = driver.findElement(By.id("fm-login-id"));
elem.clear();
elem.sendKeys("asd");
//when you want to return to the defaultContent
driver.switchTo().defaultContent();
i successfully send text to ckeditor through these command
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Automation Description");
Problem
Text "Automation Description" is succesfully passed to ckeditor.
But. it does not locate the next element
Following error is displayed
no such element: Unable to locate element:
{"method":"xpath","selector":".//*[#id='select2-state_name-container']"}
When I added Block Comment to
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Automation Description");
It successful located the next element.
Help me to add a command so that I can locate next element after texting text to ckeditor. Thank you
Here's a screen shot:
As I seeing your code you are going to switch frame first then find ckeditor and set value. But you didn't switch back to default content to find the next element that's why selenium searching the locator into already switched frame where element is not present actually, So you should try to switch back before finding element as below :-
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Automation Description");
//Now switch back first to default
driver.switchTo().defaultContent();
//Now you can go to find next element
Hope it helps...:)
I believe this has to do with your switch frame statement,
driver.switchTo().frame()
Try to switch back to 0 index frame or default one after inputting the text:
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement tinymce = driver.findElement(By.tagName("body"));
tinymce.clear();
tinymce.sendKeys("Automation Description");
driver.switchTo().frame(0); //To go back to main frame
//continue
Or better yet find the selector for the main frame window and swicth to it, but i think 0 should work in your case
Hope this helps
HTML code:
<div class="buttonBG">
<input type="button" onclick="window.location.href='?mod=eA&act=00001';" class="buttonGreen" value="TK">
<input type="button" onclick="ttoggle('carianDiv');" class="buttonGreen" value="CK">
</div>
Below is my java code, when I try with below code. Can I know whats wrong is in my selenium webdriver code:
driver.findElement(By.xpath("//input[#class='buttonGreen' and contains(#onclick, 'window.location.href='?mod=eA&act=00001')]")).click();
Try to search by value
driver.findElement(By.cssSelector("[value='TK']")).click();
And for what's wrong, you are searching for ?mod=eA&act=00001 when in the html its
?mod=eA&act=00001
Edit
Another solution is to insert the buttons to list and click by index:
List<WebElement> buttons = driver.findElements(By.className("buttonGreen"));
buttons.get(0).click();
You can also try using explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[value='TK']")).click();
This will wait up to 10 seconds for the button to be visible before clicking on it. You can change the selector or time span.
Try using XPath instead of CSS
driver.find_element(By.XPATH, '//input[#onclick=\'window.location.href=\'?mod=eA&act=00001\';\']').click()
Edit
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
NOTE: After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();
I have a page in which I click on a link which opens a new Modal window which has an iframe. I switched to the iframe and performed some validation, then click on the link in that Modal window which in turn opens a second new Modal window with an iframe. I am facing issue clicking on any element in that second new Modal window.
Here is my code.
WebElement Hotelname = driver.findElement(By.cssSelector(".hotelTitleZone2>a"));
Hotelname.click(); \\This will open a new Pop up.
driver.switchTo().frame(1);
\\perform some validation
String parentHandle = driver.getWindowHandle();
driver.findElement(By.linkText("View on a Map")).click(); \\This will open second pop up Modal window
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
driver.switchTo().defaultContent();
driver.switchTo().frame(1); \\switching to frame
driver.findElement(By.linkText("Close")).click();
When I am running this code, I am getting an error:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"link text","selector":"Close"}
I tried with or without switching to default content, without switching to frame in second Modal window but result is always the same.
Any help is appreciated ? Thanks.
My understanding is:
start from the default window
click to open the first Modal window that has an iframe
Switch to this new iframe (index = 1)
Get the ID for the current window handle, which is the default window handle
click to open the second Modal window that has a second iframe
switch to the second Modal window
switch back to the default window
switch to iframe (index = 1)
Find the button you are after
There are a few confusions here:
in step 4 above, you used String parentHandle = driver.getWindowHandle(); to store the original default window handle but you have never used it, instead, you use driver.switchTo().defaultContent();
What happened to the first Modal window after you clicked it? Did it close? if it had not closed, its iframe would still be iframe (index=1), this would explain why you could not find your button from the iframe (index=1); as your button would reside on the iframe that belongs to the second Modal window, which is likely to be iframe (index=2). You may use driver.switchTo().frame(2); to address it instead. To be sure, you can inspect HTML elements to see how many iframes are present and to which Modal windows they belong to.
Hope you will find it useful.