Java Selenium (12) Action pop-up & Smart Wait page load complete & process elements in Iframe

Source: Internet
Author: User

First, the Operation Pop-up window

Principle

In the code, through set<string> allwindowsid = Driver.getwindowhandles ();

To get a handle to all pop-up browsers, and then traverse through, using the Swithcto.window (Newwindow_handle) method. You can navigate to the new window.

HTML of the test page

<title> common Web UI element operations, and API usage </title>

<script type= "Text/javascript" >

function Open_win ()

{

window.open ("http://www.cnblogs.com")

}

</script>

<body>

<form>

<input type=button value= "open Window" onclick= "Open_win ()" >

</form>

</div>

</body>

Java Code

public static void Testmultiplewindowstitle (Webdriver driver) throws Exception

{

String url= "E:\\stashfolder\\[email protected]\\stash\\tank-moneyproject\\selenium webdriver\\alluielement.html";

Driver.get (URL);

Gets the handle of the current window

String Parentwindowid = Driver.getwindowhandle ();

System.out.println ("Driver.gettitle ():" + driver.gettitle ());

Webelement button = driver.findelement (By.xpath ("//input[@value = ' open Window ']");

Button.Click ();

set<string> Allwindowsid = Driver.getwindowhandles ();

Gets the handle of all open windows

for (String Windowid:allwindowsid) {

if (Driver.switchto (). window (Windowid). GetTitle (). Contains ("blog Park") {

Driver.switchto (). window (WINDOWID);

Break

}

}

System.out.println ("Driver.gettitle ():" + driver.gettitle ());

Switch back to the original parent window again

Driver.switchto (). window (PARENTWINDOWID);

System.out.println ("Parentwindowid:" + driver.gettitle ());

}

Second, the Intelligent waiting page loading complete

We often encounter the use of selenium to manipulate an element on the page, we need to wait for the page to be loaded after the completion of the operation. Otherwise, the element on the page does not exist and throws an exception.

Or when it comes to Ajax asynchronous loading, we need to wait for the element to be loaded before it can be manipulated.

Selenium provides a very simple, intelligent way to determine whether an element exists.

Instance requirements

Example: set_timeout.html The following HTML code, after clicking the click button for 5 seconds, a red div will appear on the page quickly, we need to write an automated script to determine whether the div exists, and then highlight the Div.

<title>set timeout</title>

<style>

. red_box {width = 20%; height:100px; border:none;}

</style>

<script>

function Show_div () {

SetTimeout ("Create_div ()", 5000);

}

function Create_div () {

D = document.createelement (' div ');

D.classname = "Red_box";

Document.body.appendChild (d);

}

</script>

<body>

<button id = "B" onclick = "show_div ()" >click</button>

</body>

Implicit wait

Webdriver Driver = new Firefoxdriver ();

Driver.get ("file:///C:/Users/Tank/Desktop/set_timeout.html");

Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);

webelement element = Driver.findelement (By.cssselector (". Red_box"));

((Javascriptexecutor) driver). Executescript ("Arguments[0].style.border = \" 5px solid yellow\ "", Element);

which

Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);

This means that for a total of 10 seconds, if the element does not exist after 10 seconds, an exception will be thrown. Org.openqa.selenium.NoSuchElementException.

Explicit wait

Explicitly waiting to use the Expectedconditions class in the self-contained method, you can make a trial wait for the judgment.

Explicit wait conditions can be customized for more complex page wait conditions.

The test code will continue to execute the subsequent test logic backwards only if the condition satisfies the explicit wait

If the maximum explicit wait time threshold is exceeded, the test program throws an exception.

public static void TestWait2 (Webdriver driver)

{

Driver.get ("E:\\stashfolder\\[email protected]\\stash\\tank-moneyproject\\ Pudong Software Park Training center \ \ My Textbook \\Selenium webdriver\\ Set_timeout.html ");

webdriverwait wait = new webdriverwait (driver, 20);

Wait.until (expectedconditions.presenceofelementlocated (By.cssselector (". Red_box"));

webelement element = Driver.findelement (By.cssselector (". Red_box"));

((Javascriptexecutor) driver). Executescript ("Arguments[0].style.border = \" 5px solid yellow\ "", Element);

}

Iii. handling elements in an Iframe

Sometimes when we locate the elements, we find that we cannot locate them. At this point you need to check if the element you want to locate is inside the IFRAME.

What is an IFRAME?

An IFRAME is an HTML that is used for Web page nesting. A Web page can be nested in another Web page and can nest many layers.

A method for entering an IFRAME is provided in the Selenium

Enter an IFRAME with ID called Framea

Dr.switchto (). FRAME ("Framea");

Back to main window

Dr.switchto (). Defaultcontent ();

Main.html

<title>FrameTest</title>

<body>

<div id= "ID1" >this is Main Page ' s div!</div>

<input type= "text" id= "Maininput"/>

<iframe id= "Framea" frameborder= "0" scrolling= "no" style= "Left:0;position:absolute;" src= "frame.html" ></ Iframe>

</body>

Frame.html

<title>this is a frame!</title>

<body>

<div id= "Div1" >this is IFRAMEs div,</div>

<input id= "Iframeinput" ></input>

</body>

Selenium code

public static void Testiframe (Webdriver driver)

{

Driver.get ("E:\\stashfolder\\[email protected]\\stash\\tank-moneyproject\\ Pudong Software Park Training center \ \ My Textbook \\Selenium webdriver\\ Frame\\main.html ");

At the time of the main window

Driver.findelement (By.id ("Maininput")). SendKeys ("main input");

When the IFRAME is not entered, the following statement will error

Driver.findelement (By.id ("Iframeinput")). SendKeys ("iframe input");

Driver.switchto (). FRAME ("Framea");

Driver.findelement (By.id ("Iframeinput")). SendKeys ("iframe input");

There is no main window at this time, the following statement will error

Driver.findelement (By.id ("Maininput")). SendKeys ("main input");

Back to main window

Driver.switchto (). Defaultcontent ();

Driver.findelement (By.id ("Maininput")). SendKeys ("main input");

}

Java Selenium (12) Action pop-up & Smart Wait page load complete & process elements in Iframe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.