Python Automated test Application-7th (Web test)--selenium Advanced article

Source: Internet
Author: User

Article 7 python Automated test application-selenium Advanced article

--lamecho

1.1 Overview

Hello everyone! I am lamecho (spicy ugly), this article will be our introduction selenium use of the last advanced article, will bring you some dry food to help everyone can deal with the actual problems encountered in the real combat.

1.2 Selenium continue the actual combat Baidu page

We continue to use Baidu as the target of actual combat. Through the previous article, we probably know the basic knowledge of the elements of the Web page and the operation of some common element types, such as buttons, input boxes, links, etc. in fact, as long as we carefully analyze its tagname (tags) can quickly find and enter or click action. So today I'm going to talk a little bit more about web or browser operations. For example, the first new type Select Selection Bar we introduced next.

1.Select and option Options

On the page if you hit the popup drop-down option, how to do this, such as the Baidu homepage of the search settings in the Red box position.

Let's first look at its page source code

#-*-encoding:utf-8-*-

From selenium import Webdriver

From Selenium.webdriver.support.select Import Select

From Selenium.webdriver.common.action_chains import Actionchains

Browser=webdriver. Firefox ()

Browser.maximize_window ()

Browser.get ("https://www.baidu.com/")

Browser.implicitly_wait (10)

El=browser.find_element_by_link_text (U ' set ')

Actionchains (browser). Move_to_element (EL). Release (). Perform ()

Browser.implicitly_wait (10)

Browser.find_element_by_link_text (U ' Search Settings '). Click ()

Browser.implicitly_wait (10)

sel=browser.find_element_by_id (' nr ')

Select (SEL). Select_by_value (' 50 ')

Sleep (2)

Select (SEL). Select_by_index (1)

Sleep (10)

Browser.quit ()

Here the main analysis of the last few lines of script, at the beginning we will first import the Select module

From Selenium.webdriver.support.select Import Select

sel=browser.find_element_by_id (' nr ')

#通过id将找到的select赋值给sel

Select (SEL). Select_by_value (' 50 ')

The #用Select (SEL) Converts the SEL to select, which can then be selected using the Select method. The usual methods are two select_by_value and Select_by_index. It is important to note that value is the value in the label, not the Text,index subscript starting from 0.

Sleep (2)

Select (SEL). Select_by_index (1)

2. Execute script command

Sometimes selenium provides us with a method that is not enough to complete some complex operations, so we can use script commands to do the corresponding actions on the page. For example, how to manipulate the browser scroll bar operation page to draw the row?

Let's start by looking at how the script executes.

Js= ' Window.scrollto (0,document.body.scrollheight); ' #下划到页面底部

Browser.execute_script (JS)

We want to execute the JS statement put into the execute_script, here is just a script application show, in fact, through the script command we can also do a lot of things. Here we need to learn more about some JavaScript-related knowledge. Like what

' var obj=document.getelementsbytagname (\ "A\"); return obj[3].text=500; '

The text value of the found a tag is changed to 500, then the text value of the corresponding element on the last page becomes 500. And this js= "return Document.readystate" will return the current page load, based on the return value of the script execution, we can determine whether the page loading is complete.

3.iframe Label

What should we do when we encounter such a tag? It is similar to the page embedded a page, can be viewed from the above to this is the Baidu map page source code, in the IFRAME framework contains a complete HTML page, we have to go on the nested frame page to locate the operation element, in fact, is very simple, All we have to do is switch browser to this frame page and use the statement browser.switch_to.frame (FRAME_ID)

FRAME_ID gets its ID in the source code in the element iframe (the IFRAME can be obtained by ID and name in Selenium's API description, but in practice the name attribute is not useful). When we are done in frame, we need to use the statement to continue executing on the original page.

Browser.switch_to_default_content ()

That is, then switch back to the original page content. Here is another case, if we encounter the ID of the IFRAME framework is dynamic, that is, each page load will dynamically specify the ID, we can do this in Python:

Frame_id=browser.find_element_by_tag_name (' iframe '). Get_attribute (' ID ')

Browser.switch_to.frame (frame_id)

This does not need to consider the ID of the IFRAME framework, because we are also dynamically getting the ID value of the current page.

4. Let the page perform keyboard operation Actionchains

First, we're going to import some new packages.

From Selenium.webdriver.common.action_chains import Actionchains

From Selenium.webdriver.common.keys import keys

The same in the Baidu search box input, the last operation of the keyboard carriage return for retrieval.

Browser.implicitly_wait (10)

el=browser.find_element_by_id (' kw ')

Actionchains (browser). Send_keys_to_element (el, ' Lamecho '). Key_down (Keys.enter). KEY_UP (Keys.enter). Perform ()

Mainly see the last line Send_keys_to_element (el, ' Lamecho ') input characters into the El element, followed by Key_down and Key_up is the action press the Bounce up Keys.enter (carriage return). This is not very convenient to write, a line of several actions. In addition, it can also realize the operation of the key combination, as long as we will press the KEY_DOWN,KEY_UP order of the button is OK.

5. New tab of Browser

The browser now supports opening multiple pages in a single window, which is what we often call tabs. So when we're doing this, the new page opens in a new tab, how do we go about finding the element in the new page? Once on the network has seen a netizen is through the keyboard of the physical key ctrl+tab to switch to the new label, admittedly we see the page is done to switch, but so we can not continue in this new page to do element search, because our browser still in the original page, this and frame switch a truth. Only by switching our browser to a new page can we continue to find elements in the new page. Let's look at how to do this in a Python script.

Browser.implicitly_wait (10)

browser.find_element_by_id (' Tangram__psp_8__username '). Send_keys (' Test ')

browser.find_element_by_id (' Tangram__psp_8__password '). Send_keys (' Test ')

#browser. find_element_by_id (' Tangram__psp_8__submit '). Click ()

Browser.find_element_by_link_text (U ' Register Now '). Click ()

While 1:

H=browser.window_handles

If Len (h) ==2:

NAME=H[1]

Break

Sleep (1)

Browser.switch_to_window (name)

Browser.implicitly_wait (10)

browser.find_element_by_id (' Tangram__psp_3__verifycode '). Send_keys (' 1234 ')

Continue the actual combat of our Baidu page, when we open the login box, our final step is to click on the "Login" button, we will comment out, change to click "Register Now" button. The actual implementation is the Baidu page is opened in the New tab page of the registration page, and then look down to our script, using the loop to execute Browser.window_handles, to find the browser window handle, when the new tab opens naturally our window will become two, Its length will naturally be equal to 2, jump out of the loop, and then we'll go through Browser.switch_to_window (name) and switch our browser to the new page to continue our element lookup work. The effect is not finally entered in the check code input 1234.

1.3 Advanced Article-end

Selenium's advanced article finally finished, in fact, is to tell the actual project encountered a little tricky problem, the operation is slightly more complicated. And I can be involved in the article in fact in the use of selenium can cover 10 of the only one or two, we need to practice, mutual encouragement.

Thank you all patiently read, I am lamecho spicy ugly.

Original articles, reproduced please indicate the source. Sina Weibo search "lamecho good " welcome everyone to tease.

Weibo: https://weibo.com/u/6017986584

Blog: http://blog.sina.com.cn/u/6017986584

Python Automated test Application-7th (Web test)--selenium Advanced 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.