Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of selenium Automation Test

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you the sample analysis of selenium automated testing, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Download the driver

Http://chromedriver.storage.googleapis.com/index.html

Download the same or recent version as the Google version.

Chrome version view

View in help

Guide package

From selenium import webdriver

Create browser objects (take chrome as an example)

Driverpath = r''# drive path (chromedriver.exe) driver = webdriver.Chrome (executable_path=driverpath)

Demo

Import timefrom selenium import webdriverurl = 'https://www.baidu.com'driverPath = rushing' # drive path driver = webdriver.Chrome (executable_path=driverPath) driver.get (url) driver.maximize_window () # maximize page time.sleep (3) driver.quit ()

Basic function

Size, position

Driver.minimize_window () # maximize page driver.minimize_window () # minimize page driver.set_window_size (200,100) # set window size print ('window size:' + str (driver.get_window_size () # window size driver.set_window_position (200,300) # set window position

Close

Driver.quit () driver.close ()

Basic operation of the page

Driver.get (url) # get a page driver.back () # fallback driver.forward () # forward

Demo

Import timefrom selenium import webdriverurl1 = 'https://www.baidu.com'url2 =' https://www.bilibili.com'driverPath = ritual C:\ Users\ 29273\ AppData\ Local\ Programs\ Python\ Python38-32\ chromedriver.exe'driver = webdriver.Chrome (executable_path=driverPath) driver.set_window_position (200,300) driver.get (url1) time.sleep (5) driver.get (url2) time.sleep (5) driver.back () time.sleep (5) driver.close ()

Element positioning and data input

Attributes in the div tag to locate

Id,classname,name,tagname,partial link text,link text,css selector

For example, the classname in the following a tag is username

Driver.find_element_by_class_name ("username")

Driver.find_element_by_class_name ("btn.btn_big.btn_error")

# partial function driver.find_element_by_xpath ('/ html/body/div [1] / div [2] / div [3] / div [2] / div [1] / dl/dd/div [1] / a') driver.find_element_by_css_selector ('body > div.wrap > div.main_wrap > div.main.cc > div.main_sidebar > div.box_wrap.user_info > dl > dd > div.name > a') driver.find _ element_by_id (value) driver.find_element_by_name (value) driver.find_element_by_class_name (value) driver.find_element_by_tag_name (value) driver.find_element_by_link_text (value) driver.find_element_by_partial_link_text (value)

Enter data into the data box

Driver.find_element_by_name ("username") .send_keys ("giao")

Click event

Driver.find_element_by_xpath ('/ * [@ id= "J_register_form"] / div/dl [5] / dd/button') .click () exercise 1 (simple web registration)

Http://47.107.178.45/phpwind/

Import timefrom selenium import webdriverurl = rust C:\ Users\ 29273\ AppData\ Local\ Python\ Python38-32\ chromedriver.exe'driver = webdriver.Chrome (executable_path=driverPath) driver.get (url) registerBtn = driver.find_element_by_xpath ('/ * [@ id= "J_login_form"] / dl/dd [2] / a') registerBtn.click () time.sleep (5) driver.find_element _ by_name ("username"). Send_keys ("giao") driver.find_element_by_name ("password"). Send_keys ("giaogiao") driver.find_element_by_name ("repassword"). Send_keys ('giaogiao') driver.find_element_by_name ("email"). Send_keys (' giao@qq.com') driver.find_element_by_xpath ('/ * [@ id= "J_register_form"] / div/dl / dd/button'). Click () time.sleep (10) driver.quit () exercise II

Open a residential website-> enter city-> number of people selected-> Click to view details

Import timefrom selenium import webdrivercity = input ('Please enter the city you want to query:') adult = input ('Please enter the number of adults:') teen = input ('Please enter the number of children:') baby = input ('Please enter the number of babies:') url = "https://www.airbnb.cn/?logo=1"driverPath = Renec:\ Users\ 29273\ AppData\ Local\ Programs\ Python\ Python38-32\ chromedriver.exe'driver = webdriver.Chrome (executable_path=driverPath ) driver.get (url) city_input = driver.find_element_by_id ("Koan-via-HeaderController__input") city_input.send_keys (city + "\ n") time.sleep (5) driver.find_element_by_name ("query") .click () time.sleep (3) for i in range (int (adult)): driver.find_element_by_xpath ('/ * [@ id= "menuItemComponent-guest_picker"] / Div/div/div/div [1] / div/div/div [1] / div/div/div/div/div/div [2] / div/div [3] / button'). Click () time.sleep (4) for i in range (int (teen)): driver.find_element_by_xpath ('/ * [@ id= "menuItemComponent-guest_picker"] / div/div/div/div [1] / div/div/div [2] / div/ Div/div/div/div/div [2] / div/div [3] / button'). Click () time.sleep (3) for i in range (int (baby)): driver.find_element_by_xpath ('/ / * [@ id= "menuItemComponent-guest_picker"] / div/div/div/div [1] / div/div/div [3] / div/div/div/div/div/div [2] / div/div [3] / button' ). Click () time.sleep (5) driver.find_element_by_class_name ("_ fhph5u") [0] .click () time.sleep (20) exercise 3

Crawl the video information that must be seen when entering a station.

Import timefrom selenium import webdriverurl = 'https://www.baidu.com'driverPath = ritual C:\ Users\ 29273\ AppData\ Programs\ Python\ Python38-32\ chromedriver.exe'driver = webdriver.Chrome (executable_path=driverPath) res = driver.find_elements_by_class_name ("video-card") for i in range (len (res)): print (' th'+ str (I) + 'video') print ('upmaster:' + str (resi]) .find _ element_by_class_name ("up-name__text") .text) print ('video name:' + str (res[ I]. Find _ element_by_class_name ("video-name") .text) print ('video playback:' + str (res[ I] .find _ element_by_class_name ("play-text") .text)) print ('number of comments:' + str (res[ I]) .find _ element_by_class_name ("like-text") .text) print ('note:' + str (res[ I]. Find _ element_by_class_name ("history-hint") .text)) the above is an example analysis of selenium automated testing Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report