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

How to use python Web applications to test selenium libraries

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use python Web applications to test selenium libraries". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use python Web applications to test selenium libraries.

Catalogue

Declare browser objects

Second, visit the page and get the web page html

Third, find elements

IV. Element interaction-automatic search by passing keywords into the search box

Interactive action, drive the browser to carry out the action, simulate the drag action, and attach the action to the serial execution in the action chain.

VI. Execute JavaScript

7. Get element information

VIII. Frame operation

9. Waiting

11. Forward and backward-realize the browser's forward and backward to browse different web pages

12. Cookies

XIII. Exception handling

Simulate the browser to load the web page, when requests,urllib can not get the web page content normally

Declare browser objects

Note: 1. Do not name the Python file name or package name selenium, which will cause it to fail to import.

From selenium import webdriver#webdriver can be thought of as the driver of the browser. To drive the browser, webdriver must be used to support multiple browsers. Here, take Chrome as an example: browser = webdriver.Chrome () 2. Visit the page and get the web page htmlfrom selenium import webdriverbrowser = webdriver.Chrome () browser.get ('https://www.taobao.com')print(browser.page_source)#browser.page_source is all the htmlbrowser.close () that gets the web page. 3. Find the element

Single element

From selenium import webdriverbrowser = webdriver.Chrome () browser.get ('https://www.taobao.com')input_first = browser.find_element_by_id (' q') input_second = browser.find_element_by_css_selector ('# q') input_third = browser.find_element_by_xpath ('/ * [@ id= "Q"]') print (input_first,input_second,input_third) browser.close ()

Common search methods

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

You can also use a common method

From selenium import webdriverfrom selenium.webdriver.common.by import Bybrowser = webdriver.Chrome () browser.get ('https://www.taobao.com')input_first = browser.find_element (BY.ID,'q') # pass the name in the first parameter and the specific parameter print (input_first) browser.close () in the second parameter

Multiple elements, elements multiple s

Input_first = browser.find_elements_by_id ('q') 4. Element interaction-search box passes keywords for automatic search from selenium import webdriverimport timebrowser = webdriver.Chrome () browser.get ('https://www.taobao.com')input = browser.find_element_by_id (' q') # find search box input.send _ keys ('iPhone') # transfer keyword time.sleep (5) input.clear () # clear search box input.send _ keys (' men's underwear') button = browser .find _ element_by_class_name ('btn-search') # find the search button button.click ()

More operations: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement# can have properties, screenshots, etc.

Fifth, interactive action, drive the browser to carry out the action, simulate the drag action Attach an action to the action chain to serially execute from selenium import webdriverfrom selenium.webdriver import ActionChains# into the action chain browser = webdriver.Chrome () url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'browser.get(url)browser.switch_to.frame('iframeResult')# switch to iframeResult frame source = browser.find_element_by_css_selector (' # draggable') # find the dragged object target = browser.find_element _ by_css_selector ('# droppable') # find the target actions = ActionChains (browser) # declare the actions object actions.drag_and_drop (source Target) actions.perform () # execute the action

More actions: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

VI. Execute JavaScript

Some actions may not provide api, such as the progress bar drop-down. In this case, we can execute JavaScript through code.

From selenium import webdriverbrowser = webdriver.Chrome () browser.get ('https://www.zhihu.com/explore')browser.execute_script('window.scrollTo(0, document.body.scrollHeight)') browser.execute_script ('alert ("To Bottom")') 7. Get element information

Get attribute

From selenium import webdriverfrom selenium.webdriver import ActionChainsbrowser = webdriver.Chrome () url = 'https://www.zhihu.com/explore'browser.get(url)logo = browser.find_element_by_id (' zh-top-link-logo') # get the website logoprint (logo) print (logo.get_attribute ('class')) browser.close ()

Get text value

From selenium import webdriverbrowser = webdriver.Chrome () url = 'https://www.zhihu.com/explore'browser.get(url)input = browser.find_element_by_class_name (' zu-top-add-question') print (input.text) # input.text text value browser.close ()

# get Id, location, tag signature, size

From selenium import webdriverbrowser = webdriver.Chrome () url = 'https://www.zhihu.com/explore'browser.get(url)input = browser.find_element_by_class_name (' zu-top-add-question') print (input.id) # get idprint (input.location) # get position print (input.tag_name) # get signature print (input.size) # get size browser.close () VIII, Frame operation

Frame is equivalent to an independent web page. If you look for a subclass in the parent class network frame, you must switch to the frame of the subclass. If the subclass is looking for a parent class, you need to switch first.

From selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionbrowser = webdriver.Chrome () url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'browser.get(url)browser.switch_to.frame('iframeResult')source = browser.find_element_by_css_selector (' # draggable') print (source) try: logo = browser.find_element_by_class_name ('logo') except NoSuchElementException: print (' NO LOGO') browser.switch _ to.parent_frame () logo = browser.find_element_by_class_name ('logo') print (logo) print (logo.text) IX. wait for

Implicit waiting

When implicit wait is used to execute the test, if WebDriver does not find the element in DOM, it will continue to wait. If the set time is exceeded, an exception will be thrown that the element cannot be found.

In other words, when the lookup element or element does not appear immediately, the implicit wait will wait a period of time to find the DOM. The default time is 0.

From selenium import webdriverbrowser = webdriver.Chrome () browser.implicitly_wait (10) # an exception will be thrown after waiting for 10 seconds to load and return browser.get ('https://www.zhihu.com/explore')input = browser.find_element_by_class_name (' zu-top-add-question') print (input)) normally within 10 seconds.

Explicit wait

Specify a waiting condition and a maximum waiting time. The program will determine whether the condition is satisfied within the waiting time. If it is satisfied, it will return. If it is not satisfied, it will continue to wait, and an exception will be thrown after the waiting time.

From selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECbrowser = webdriver.Chrome () browser.get ('https://www.taobao.com/')wait = WebDriverWait (browser, 10) input = wait.until (EC.presence_of_element_located ((By.ID,' q')) button = wait.until (EC.element_to_be_clickable ((By.CSS_SELECTOR,'. Btn-search')) print (input, button)

The title of title_is is something.

The title_contains title contains something.

Presence_of_element_located elements are loaded out and passed in location tuples, such as (By.ID,'p')

Visibility_of_element_located element visible, passing in location tuple

Visibility_of visible, passing in element object

All elements of presence_of_all_elements_located are loaded out

Text_to_be_present_in_element an element text contains some text

Text_to_be_present_in_element_value an element value contains some text

Frame_to_be_available_and_switch_to_it frame load and switch

Invisibility_of_element_located element is not visible

Element_to_be_clickable element clickable

Staleness_of determines whether an element is still in DOM, and can determine whether the page has been refreshed

Element_to_be_selected element is selectable, pass element object

Element_located_to_be_selected element is selectable, passing in location tuple

Element_selection_state_to_be passes in the element object and the state. Equal returns True, otherwise returns False.

Element_located_selection_state_to_be passes in location tuple and status. Equal returns True, otherwise returns False.

Whether Alert appears in alert_is_present

Details: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions

11. Forward and backward-realize the forward and backward of the browser to browse different web pages import timefrom selenium import webdriverbrowser = webdriver.Chrome () browser.get ('https://www.baidu.com/')browser.get('https://www.taobao.com/')browser.get('https://www.python.org/')browser.back()time.sleep(1)browser.forward()browser.close() 12. Cookiesfrom selenium import webdriverbrowser = webdriver.Chrome () browser.get ('https://www.zhihu.com/explore')print(browser.get_cookies())browser.add_cookie({'name':' name' 'domain':' www.zhihu.com', 'value':' germey'}) print (browser.get_cookies ()) browser.delete_all_cookies () print (browser.get_cookies ())

Tab management adds browser window

Import timefrom selenium import webdriverbrowser = webdriver.Chrome () browser.get ('https://www.baidu.com')browser.execute_script('window.open()')print(browser.window_handles)browser.switch_to_window(browser.window_handles[1])browser.get('https://www.taobao.com')time.sleep(1)browser.switch_to_window(browser.window_handles[0])browser.get('http://www.fishc.com') XIII. Exception handling from selenium import webdriverbrowser = webdriver.Chrome () browser.get ('https://www.baidu.com')browser.find_element_by_id('hello')from selenium import webdriverfrom selenium.common.exceptions import TimeoutException NoSuchElementExceptionbrowser = webdriver.Chrome () try: browser.get ('https://www.baidu.com')except TimeoutException: print (' Time Out') try: browser.find_element_by_id ('hello') except NoSuchElementException: print (' No Element') finally: browser.close () so far I believe you have a better understanding of "how to use python Web applications to test selenium libraries", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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