Selenium Webdriver Advanced Usage-Select the appropriate webdrvier_selenium

Source: Internet
Author: User
Tags xpath

Turn from: http://www.open-open.com/lib/view/open1402625607337.html

Choose the right Webdrvier

Webdriver is an interface that has several implementations, namely Htmlunitdrvier, Firefoxdriver, Internetexplorerdriver, Chromedriver, Operadriver, except Internetexplorerdriver can only run on Windows platforms, and other webdriver can span platforms.

If the pursuit of running speed, htmlunitdriver is preferred, but it does not run the interface, can not see the effect of real-time operation. If you want to see the effect, you can use Firefoxdrvier, it will actually open the browser, running on the screen, so we can monitor the location of page elements, CSS values, etc., but the price is slow.

Here for simplicity, use Htmlunitdriver.

Webdriver  Driver = new Htmlunitdriver ();

Navigation

With the Webdriver instance, the first thing to do is to open the page. The general practice is to call the Get method:

Driver.get ("http://www.yeetrack.com");

Webdriver will wait until the page is loaded (that is, the "onload" method is released).

Interacting with the page just to open the page is not enough, what we need is to do the operation on the page, further speaking is to manipulate the HTML elements in the page, such as:

<input type= "text" name= "passwd" id= "Passwd-id"/>
To manipulate page elements, you first have to locate the elements, which can be positioned in several ways, as follows:
webelement element;
element = Driver.findelement (By.id ("Passwd-id"));
element = Driver.findelement (By.name ("passwd"));
element = Driver.findelement (By.xpath ("//input[@id = ' Passwd-id ')");

When locating elements, there are two points to note. When a link is positioned by the value of text, the text must match exactly, and when the element is positioned through XPath, if it matches to more than one, only the first result is returned, and if there is no match, the nosuchelementexception exception is thrown.
Webdriver has the "Object-baseed" API, and we can use webelement to represent all the page elements. The Webelement class has many methods of manipulating elements, but some methods may not be valid for the current element, so don't worry, Webdriver will try to help us deal with it. For example, for a "meta" tag element, we call the Webelement "setselected ()" method, and Webdriver throws an exception.

We're going to go to an element, and next, we might want to do something, like typing some characters:

Element.sendkeys ("Sone text");

Of course, we can also simulate keyboard input through the "Keys" class:

Element.sendkeys ("and some", keys.arrow_down);

Remember, the strings we enter into these elements are not automatically emptied, meaning that the call to the "SendKeys ()" Method is to append a string to the element. If you want to empty an input label or textarea:

Element.clear ();

Fill out the form

Already know how to type a string, what about the other elements? such as checkboxes, how to select the Option tab, and how to set setselected (). The following is an example of processing a SELECT tag:

Webelement select = Driver.findelement (By.xpath ("//select"));
list<webelement> alloptions = select.findelements (by.tagname ("option"));
for (webelement option:alloptions)
{
    System.out.println (String.Format (' Value is '%s, Option.getattribute (" Value "));
    Option.click ();
}

The code above, first locates a SELECT element, then gets all option below it, and prints it out to perform a click operation. Obviously, this method is not the best choice if you want to select a check. In fact, Webdrvier defines the Select class, which provides some quicker ways, such as "Selectbyindex ()", "Selectbyvalue ()".

The form is completed and then is submitted, and we can submit it by clicking the Submit button.

Driver.findelement (By.id ("submit")). Click ();
Alternatively, Webelement provides the "Submit ()" method, and if the current element is in the form form, calling the element's Submit method, Webdriver automatically submits the form, and if not in the form form, throws a " Nosuchelementexception "Exception.

Element.submit ();

Drag and drop

Webdriver allows us to drag and drop elements, such as dragging an element to another place, or dragging an element onto another element.

webelement element = Driver.findelement (By.name ("source"));
webelement element = Driver.findelement (By.name ("target"));
(New Actions (Driver)). Draganddrop (element, target). Perform ();

Toggle window or Frame

For today's Web applications, there are very few single window cases in which the other frame is embedded. Webdrvier supports switching to other windows using the "SwitchTo" method.

Driver.switchto (). window. ("Windowname");

After the above code executes, driver will switch to windowname this window. But how to get windowname? Here's an example:

<a href= "http://www.yeetrack.com" target= "Windowname" > click to open a new window </a>

Also, we can switch through "window handle", and we can also iterate through all the current Windows

For (String handle:driver.getWindowHandles ()) {    Driver.switchto (). window (handle);}

Toggle frame (or iframe)

Driver.switchto (). FRAME ("framename");
SwitchTo () can be chained or switched by index

Drvier.switchto (). FRAME ("FrameName")
      . SwitchTo (). FRAME (0)
      . SwitchTo (). FRAME ("child");
After the above statement is executed, the driver enters the "FrameName" frame, then enters the first frame and then into the frame where the name of the child frame is "children". If you want to go back to the top frame, execute the following code:

Driver.switchto (). Defaultcontent ();

Navigation: History and location

Earlier, we used the Webdriver get method to open the "http://www.yeetrack.com". Webdriver provides a number of interfaces, one of which is as follows:

Driver.navigate () to ("http://www.yeetrack.com");
The navigate (). to () and get () methods implement exactly the same functionality, except that "get ()" is spelled more simply.

"Navigete" interface, you can also control the browser's forward and backward:

Driver.navigate (). Forward ();
Driver.navigate (). back ();
Note that this feature is entirely dependent on the underlying browser. If the behavior of the two browsers is inconsistent, then the effect of the code execution will not be the same.

Cookies

Browser security test, cookies are not cross-domain, so you want to operate a domain name cookie, first you have to switch to a domain name.

Open the domain name
driver.get ("http://www.yeetrack.com");

Add Cookie Cookie
= new Cookie ("UUID", "Abcdedfjeiajfei");
Driver.manage (). Addcookie (cookie);

The cookie
Set (cookie) allcookies = Driver.manage () that traverses the current domain name (). GetCookies ();
for (Cookie loadedcookie:allcookies)
{
    System.out.println (String.Format ("%s--->%s") Loadedcookie.getname (), Loadedcookie.getvalue ()));


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.