Detailed explanation of Webdriver API (1)

 

illustrate

Detailed explanation of Webdriver API, based on python3, unittest framework, driver version and browser selection. This content requires a simple understanding of the unittest framework of python3. I will not go into details here. If you don’t understand it, you can just search it on Baidu. Without further ado, let’s get straight to the point!

Simple code framework template

1 from selenium import webdriver 
 2 import unittest 
 3 
 4 class MyTest(unittest.TestCase): 
 5 
 6 def setUp(self): 
 7 self.driver = webdriver.Chrome() 
 8 
11 def testFunc(self): # All the following example codes Directly replace this function or add the function directly to the code 
12 pass 
13 def tearDown(self): 
14 self.driver.quit() 
15 
16 
17 if __name__ == '__main__': 
18 unittest.main()

①Visit a certain website

Purpose: Open the specified URL

Test address: https:www.baidu.com

Call API instance code

1 def testOpenBaiduUrl(self): 
 2 ''' 
 3 1. Access the specific address 
 4 :return: 
 5 ''' 
 6 baseUrl = 'https://www.baidu.com/' 
 7 baseTitle = 'Baidu' 
 8 self. driver.get(baseUrl)# Visit Baidu URL 
 9 currTitle = self.driver.title # Get the title of the current page 
10 self.assertIn(baseTitle, currTitle, msg='Page jump failed') # Assert

② Forward, backward and refresh the page of the web page

Purpose: Simulate the forward and backward functions of the browser

Test address:

https://www.sogou.com;https://www.baidu.com

Call API instance code

1 def testBackForwardRefresh(self): 
 2 ''' 
 3 Forward, backward, refresh the current page 
 4 :return: 
 5 ''' 
 6 baseBaiduUrl = 'https://www.baidu.com/' 
 7 baseSogouUrl = 'https:// www.sogou.com/' 
 8 self.driver.get(baseBaiduUrl) 
 9 self.driver.get(baseSogouUrl) 
10 self.driver.back() # Back 
11 print(self.driver.current_url) 
12 self.driver.forward () # Go forward 
13 print(self.driver.current_url) 
14 self.driver.refresh() #Refresh the page

③ Manipulate the browser window position

Purpose: Maximize the window, obtain the browser position coordinates, and set the browser window position

Test address: https://www.baidu.com

Call API instance code

1 def testWindow(self): 
 2 ''' 
 3 Maximize the window, get the position of the current window, and set the position of the current window 
 4 :return: 
 5 ''' 
 6 baseBaiduUrl = 'https://www.baidu.com/' 
 7 self.driver.get(baseBaiduUrl) 
 8 position = self.driver.get_window_position() # Get the position coordinates of the current window 
 9 print('The abscissa of the current window is {}'.format(position['x'])) 
10 print('The ordinate of the current window is {}'.format(position['y'])) 
11 self.driver.set_window_position(400, 200) # Set the position of the window 
12 print('The position of the window after setting Coordinates: {}'.format(self.driver.get_window_position())) 
13 self.driver.maximize_window() #Maximize the window

④ Manipulate the size of the browser window

Purpose: Get the size of the current window and set the size of the current window

Test address: https://www.baidu.com

Call API instance code

1 def testWindowSize(self): 
 2 ''' 
 3 Get the size of the current window and set the size of the current window 
 4 :return: 
 5 ''' 
 6 baseBaiduUrl = 'https://www.baidu.com/' 
 7 self.driver .get(baseBaiduUrl) 
 8 #Get the size of the current window 
 9 windowSize = self.driver.get_window_size('current') 
10 print('The width of the current window is {}'.format(windowSize['width'])) 
11 print ('The height of the current window is {}'.format(windowSize['height'])) 
12 #Set the size of the current window 
13 self.driver.set_window_size(width=200, height=400, windowHandle='current') 
14 print(self.driver.get_window_size('current'))

⑤Get ​​the Title attribute value of the current page

Purpose: Get the title attribute value of Baidu page

Test address: https://www.baidu.com

Call API instance code

1 def testGetBaiduTitle(self): 
 2 ''' 
 3 Get the title attribute value of the page 
 4 :return: 
 5 ''' 
 6 baseBaiduUrl = 'https://www.baidu.com/' 
 7 self.driver.get(baseBaiduUrl) 
 8 bdTitle = self.driver.title # Get the title attribute value of the Baidu page 
 9 print(bdTitle) 
10 self.assertEqual(bdTitle,'Baidu, you will know',msg='The page title is incorrect'

⑥Get the url address and page source code of the page

Purpose: Obtain the address and source code of Baidu page

Test address: https://www.baidu.com

Call API instance code

1 def testGetBaiduUrlSourceCode(self): 
 2 ''' 
 3 Get the url and page source code of the page 
 4 :return: 
 5 ''' 
 6 baseBaiduUrl = 'https://www.baidu.com/' 
 7 self.driver.get(baseBaiduUrl ) 
 8 bdUrl = self.driver.current_url # Get the url address of the current page 
 9 print(bdUrl) 
10 self.assertEqual(bdUrl,'https://www.baidu.com/',msg='The current page url is incorrect' ) 
11 sourceCode = self.driver.page_source # Get the source code of the current page v 
12 print(sourceCode)

⑦Get and switch browser window handles

Purpose: Get the current window handle, get all window handles, and switch between handles

Test address: https://www.baidu.com

Call API instance code

1 def testGetwindowHandle(self): 
 2 ''' 
 3 Get the handle of the current page, switch window 
 4 :return: 
 5 ''' 
 6 import time 
 7 baseBaiduUrl = 'https://www.baidu.com/' 
 8 self.driver .get(baseBaiduUrl) 
 9 self.driver.maximize_window() 
10 # Get the current window handle 
11 current_handle = self.driver.current_window_handle 
12 print(current_handle) 
13 #Enter selenium in the Baidu search box and click Baidu 
14 self.driver.find_element_by_id( 'kw').send_keys('selenium') 
15 self.driver.find_element_by_id('su').click() 
16 time.sleep(3) 
17 #Click selenium's Baidu Encyclopedia link
18 self.driver.find_element_by_partial_link_text('Baidu Encyclopedia').click() 
19 #Get the handles of all windows 
20 all_handles = self.driver.window_handles 
21 print(all_handles) 
22 #Print the handle of the new window 
23 print(self.driver. window_handles[-1]) 
24 for handle in all_handles: 
25 # Switch to a new window 
26 if handle != current_handle: 
27 self.driver.switch_to.window(handle) 
28 self.driver.find_element_by_link_text('The English name of the element selenium ').click() 
29 # Return to the original window 
30 self.driver.switch_to.window(current_handle) 
31 sendKeys = self.driver.find_element_by_id('kw') 
32 sendKeys.clear() 
33 sendKeys.send_keys('python')

⑧Get basic information about page elements

Purpose: Get the tagname, size, text of a certain element on the page

Test address

https://www.baidu.com

1 def testGetElementInfo(self): 
2 
3 baseBaiduUrl = 'https://www.baidu.com/' 
4 self.driver.get(baseBaiduUrl) 
5 self.driver.maximize_window() 
6 element = self.driver.find_element_by_xpath(" //a[text()='News']") 
7 print('My tag_name is {}, my text is {}, my size is {}'.format(element.tag_name,element.text, element.size))

Output: My tag_name is a, my text is news, and my size is {'height': 24, 'width': 26}

⑨Get the CSS attribute value of the element

Purpose: Get the CSS attribute value of Baidu input box

Test address

https://www.baidu.com

1 def testGetCssInfo(self): 
 2 ''' 
 3 Get the css attribute value of the element 
 4 :return: 
 5 ''' 
 6 baseBaiduUrl = 'https://www.baidu.com/' 
 7 self.driver.get(baseBaiduUrl) 
 8 self.driver.maximize_window() 
 9 element = self.driver.find_element_by_id('kw') 
10 print(element.value_of_css_property('height'))# Get the height of the search box 
11 print(element.value_of_css_property('width') )# Get the width of the search box 
12 print(element.value_of_css_property('font-family'))# Get the font entered in the search box

Output:

22px
500px
arial

⑩Enter the specified content in the input box and clear the content in the input box

Purpose: Enter python in the Baidu input box and clear the content

Test address

https://www.baidu.com

1 def testInputClear(self): 
 2 ''' 
 3 Get the css attribute value of the element 
 4 :return: 
 5 ''' 
 6 import time 
 7 baseBaiduUrl = 'https://www.baidu.com/' 
 8 self.driver.get (baseBaiduUrl) 
 9 self.driver.maximize_window() 
10 element = self.driver.find_element_by_id('kw') 
11 element.send_keys('python')# Enter the specified content 
12 time.sleep(5) 
13 element.clear() # Clear the input box

Summarize

There are a total of 10 examples above, involving APIs such as get(), send_keys(), clear(), text, maximum_window(), current_url, back(), forword(), refresh(), get_window_position(), get_window_size(), There are more than a dozen methods such as set_window_position, title, current_window_handle, window_handles, value_of_css_property(), etc., which are often used in actual work. I hope it will be helpful to everyone, and other API usages will be updated in the future.


If you want to learn automated testing, then the following set of videos should help you a lot 

How to force yourself to finish learning automated testing in one month, and get a job immediately after learning. Even a novice can get it at his fingertips. You can take it away without any thanks, and you are allowed to have sex for free...

Finally, I will share with you some documents and learning materials that I have accumulated and organized. If you need them, you can just get them directly. The above content should be the

most comprehensive and complete preparation warehouse for software testing friends. For the better To organize each module carefully, I also referred to many high-quality blog posts and projects on the Internet, trying not to miss any knowledge point. Many friends relied on these contents to review and got offers from major manufacturers such as BATJ. This warehouse has also helped I have learned a lot about software testing, and I hope it can help you too.

Guess you like

Origin blog.csdn.net/weixin_56331124/article/details/133279569