Selenium Python bindings document 2

Source: Internet
Author: User
Tags selenium python

3 jump

The first thing to do with Webdriver is to jump to a page. The general method is to call the get method.

Driver. get (Http://www.python.org")

The WebDriver waits until the page is fully loaded before returning control over the test or script. However, if the page uses a lot of AJAX, WebDriver may not know when the page will be fully loaded, so it is not worth doing so. If you need to ensure that such a page is fully loaded, you can use the waits method.

3.1 interaction with pages

It is not very useful to be able to reach the page. What we really want to do is to interact with the page. Or, more precisely. And HTML elements in the page. First, we need to find an element. WebDriver provides many methods to find elements. Assume that an element is defined:

<input type="text" name="passwd" id="passwd-id" />

You can find this element using the following methods:

Element = driver. find_element_by_id ("passwd_id ")

Element = driver. find_element_by_name ("passwd ")

Element = driver. find_element_by_xpath ("/input [@ id = 'passwd-id']")

You can also find a link in text, but be careful! This text must be exactly matched! Be careful when using XPATH in WebDriver. If multiple elements meet the query conditions, only the first element that meets the conditions is returned. If no element is found, NoSuchElementException is reported.

WebDriver has an object-based API. We use the same interface to represent various elements. This means that you may see many methods that can be called when using the IDE's Automatic completion function. But not all of them work or are feasible. But don't worry! WebDriver will try to do the right thing, and if you call a method that does not work, an exception will be thrown.

You get an element. What can you do? First, you may want to enter a text in the input box.

Element. send_keys ("some text ")

You can use the Keys class to simulate the press the Arrow

Element. send_keys ("and some", Keys. ARROW_DOWN)

Send_keys can be called on any element, which makes it possible to test the keyboard shortcut keys, such as those used on Gmail. One side effect is that the characters entered in the input box are not automatically cleared. Instead, your input will be added after the existing text. You can easily use the clear method to clear the content of the input box or input area.

Element. clear ()

3.2 fill form

We have seen how to input characters in the input box, but what about other elements? You can select the status of the Selection box. You can use setSelected to set the option label to the selected status. Processing the SELECT option is not too bad.

Select = driver. find_element_by_xpath ("// select ")

All_options = select. find_element_by_tag_name ("option ")

For option in all_options:

Print "value is: % s" % option. get_attribute ("value ")

Option. click ()

This will find the first SELECT element on the page, and print their values through each OPTION in a loop, and then SELECT each element in sequence.

After filling the form, you may want to submit it. One way is to find the submit button and click it.

# Assume the button has the ID "submit"

Driver. find_element_by_id ('submit '). click ()

Alternatively, WebDriver has a convenient submit Method on each element. If you call this method on a form element, WebDriver queries the DOM tree until it finds the form containing it and then calls submit. If the element is not in form, NoSuchElementExcellent will be thrown.

Element. submit ()

 

3.3 tops and tubs

Element = driver. find_element_by_name ("source ")

Target = driver. find_element_by_name ("target ")

From selenium. webdriver import ActionChains

Action_chains = ActionChains (driver)

Action_chains.drag_and_drop (element, target)

 

3.4 move between window and Frame

In today's web applications, few windows have no frames. WebDriver supports moving in the name window using the switch_to_window method.

Driver. switch_to_window ("windowName ")

All calls to the driver are now interpreted as directed to a specific window. But how do I know the window name? Take a look at the javascript or link that opens the window.

<A href=”somewhere.html "target =" windowName "> Click here to open a new window </a>

You can send a window handle to the switch_to_window () method. By knowing this, You Can iteratively access each Opened Window

For handle in driver. window_handlers:

Driver. switch_to_window (handle)

You can also switch between frames.

Driver. switch_to_frame ("frameName ")

You can also access the sub-frame by using the point decomposition path, and you can specify the index to determine the frame.

Driver. switch_to_frame (frameName.0.child)

 

3.5 pop-up window

Selenium WebDriver provides built-in support for processing the pop-up window. When you activate an action, you can open a pop-up window. You can access alert in the following ways.

Alert = driver. switch_to_alert ()

This will return the currently opened warning object. With this object, you can accept, cancel, read its content, or even input the content in the prompt. This excuse is being warned, confirmed, and prompted to work as well.

 

3.6 navigation: History and positioning

Previously, we used the get method to cover the jump. As you can see, WebDriver has many smaller interfaces in the task set. navigation is a useful task. To jump to a page, you can use the get method: driver. get ("http://www.example.com ")

To move forward or backward in the browser history:

Driver. forward ()

Driver. back ()

Note that this function depends entirely on the potential driver.

 

3.7 Cookies

Driver. get (http://www.example.com ")

# Now set the cookie. This one is valid for the entire domain

Cookie = {"key": "value "}

Driver. add_cookie (cookie)

 

# And now output all the available cookies for the current URL

All_cookies = driver. get_cookies ()

For cookie_name, cookie_value in all_cookies.items ():

Print "% s-> % s", cookie_name, cookie_value

 

4 positioning Element

There are many ways to locate elements on the page. You can use the one most suitable for your case. Selenium provides the following methods to locate elements on the page

Find_element_by_id

Find_element_by_name

Find_element_by_xpath

Find_element_by_link_text

Find_element_by_partial_link_text

Find_element_by_tag_name

Find_element_by_class_name

Find_element_by_css_selector

 

Multiple elements are found (these methods return a list)

Find_elements_by_name

Find_elements_by_xpath

Find_elements_by_link_text

Find_elements_by_partial_link_text

Find_elements_by_tag_name

Find_elements_by_class_name

Find_elements_by_css_selector

4.1 identify elements by ID

This method is used when you know the id attribute of an element. With this policy, the first element with the id attribute matching the positioning will be returned. If no element has the id attribute, NoSuchElementException will be thrown.

For example, consider the page source file

<Html>

<Body>

<Form id = "loginForm">

<Input name = "username" type = "text"/>

<Input name = "password" type = "password"/>

<Input name = "continue" type = "submit" value = "Login"/>

</Form>

</Body>

<Html>

The Form element can be located as follows: login_form = driver. find_element_by_id ("loginForm ")

 

4.2 locate by name

This method is used when you know the name attribute of an element. With this policy, the first element with the name attribute matching the positioning will be returned. If no element has the name attribute, NoSuchElementException will be thrown.

For example, consider the page source file

<Html>

<Body>

<Form id = "loginForm">

<Input name = "username" type = "text"/>

<Input name = "password" type = "password"/>

<Input name = "continue" type = "submit" value = "Login"/>

<Input name = "continue" type = "button" value = "Clear"/>

</Form>

</Body>

<Html>

The username and password elements can be located as follows:

Username = driver. find_element_by_name ("username ")

Password = driver. find_element_by_name ("password ")

Continue = driver. find_element_by_name ("Continue ")

 

4.3 positioning through xpath

Xpath is a language used to locate nodes in XML documents.

For example, consider the page source file

<Html>

<Body>

<Form id = "loginForm">

<Input name = "username" type = "text"/>

<Input name = "password" type = "password"/>

<Input name = "continue" type = "submit" value = "Login"/>

<Input name = "continue" type = "button" value = "Clear"/>

</Form>

</Body>

<Html>

The Form element can be located as follows:

Login_form = driver. find_element_by_xpath ("/html/body/form [1]")

Login_form = driver. find_element_by_xpath ("// form [1]")

Login_form = driver. find_element_by_xpath ("// form [@ id = 'loginform']")

The Username can be located as follows:

Username = driver. find_element_by_xpath ("// form [input/@ name = 'username']")

Username = driver. find_element_by_xpath ("// form [@ id = 'loginform']/input [1]")

Username = driver. find_element_by_xpath ("// input [@ name = 'username']")

 

4.4 define a hyperlink through link text

<Html>

<Body>

<P> Are you sure you want to do this? </P>

<A href = "continue.html"> Continue </a>

<A href = "cancel.html"> Cancel </a>

</Body>

<Html>

The Continue link can be located as follows:

Continue_link = driver. find_element_by_link_text ('contine ')

Continue_link = driver. find_element_by_partial_link_text ('conti ')

 

For the original English text, see here

Related Article

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.