Why do you choose Python?
Before the rookie series is based on Java, a year did not learn to forget almost, the current product part of the test is also written in Python, and the team is also promoting Python, in fact, for testers, Python is also quite popular. Easy to learn, easy to use. The probability of Python appearing is also quite high when you flip through the test jobs.
Platform setup:
Already introduced in the previous article, if you also want to experience the charm of automation, then quickly build their own environment bar ~!
Selenium + Python automated test environment setup
First script:
Let's see how sexy Python looks on the selenium webdriver:
# coding = Utf-8 from
Selenium import webdriver
browser = webdriver. Firefox ()
browser.get ("http://www.baidu.com")
browser.find_element_by_id ("kw"). Send_keys ("Selenium")
browser.find_element_by_id ("su"). Click ()
browser.quit ()
What do you think? Believe that people do not understand the code can understand, but still please allow me to nag at the meaning of each sentence:
# coding = Utf-8
Can add can not add, developers like to add, to prevent garbled well.
From selenium import Webdriver
To use the function in Selenium's webdriver, first get the package in.
Browser = Webdriver. Firefox ()
Which browser do we need to manipulate? Firefox, of course, can also be replaced by IE or Chrome. Browser can be arbitrarily taken, but later to use it to manipulate a variety of function execution.
browser.find_element_by_id ("kw"). Send_keys ("Selenium")
A control has a number of property IDs, name, (can also be positioned in other ways), Baidu input box ID called kw, I want to enter the selenium in the input box. How natural the language is!
browser.find_element_by_id ("su"). Click ()
The ID of the search button is Su, and I need to click the button (click ()).
Browser.quit ()
Exit and close every driver associated with the window, and it has a similar cousin.
Browser.close ()
Close the current window and see what you need.
Add Hibernate
What the? You said that too quickly did not see the browser's operation. Please time and let him run slowly.
# coding = Utf-8 from
Selenium import webdriver
import time #调入time函数
browser = webdriver. Firefox ()
browser.get ("http://www.baidu.com")
Time.sleep (0.3) #休眠0.3 seconds
Browser.find_element_by_ ID ("kw"). Send_keys ("Selenium")
browser.find_element_by_id ("su"). Click ()
time.sleep (3) # hibernate 3 seconds
Browser.quit ()
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/
Time.sleep () function to plug, where too fast, no longer need to worry about the operation of the script to see the process.
In fact, the true purpose of this function is not to give us a look at the running process of the script, sometimes the network reason, or the page load slow. If the Search box input box entered the selenium, the search button has not yet been loaded, then the script will complain. adding Time.sleep () in the right place helps reduce the failure of script execution caused by network causes;
Output
What the? When you run the script, you're on a toilet and you don't know if the script just ran successfully. Print out the title of the page you just visited.
# coding = Utf-8 from
Selenium import webdriver
driver = webdriver. Chrome ()
driver.get (' http://www.baidu.com ') print
driver.title # prints out the title of the page
Driver.quit ()
Although I didn't see the execution of the script, I saw in the execution results
>>>
Baidu a bit, you will know
The description page is correctly opened by me.