Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Selenium Client Driver for Python (1)

Shulou Source: shulou.com Published: 2022-06-03 06:29:53 09月25日 Update

A brief introduction to Introduction

Introduce Selenium for Python.

1.selenium is used for web automated testing. Python2.7, 3.4 + and above are supported.

II. Selenium installation

You can install it online using pip install-U selenium, or you can download a source package, such as selenium-3.13.0.tar.gz, and install it using Python setup.py install after decompression.

Third, driver installation

Selenium needs to install a driver to open and close browsers, such as Firefox, need geckodriver, put this program in the Firefox installation directory, and configure Firefox's environment variables.

IV. Examples

1. Instance 0:

(1) Open a new Firefox browser

(2) Open the web page according to URL

From selenium import webdriver

Browser = webdriver.Firefox ()

Browser.get ('http://seleniumhq.org')

two。 Example 1:

(1) Open a new Firefox browser

(2) load the Yahoo home page

(3) find "seleniumhq"

(4) close the browser

From selenium import webdriver

From selenium webdriver.common.keys import Keys

Browser = webdriver.Firefox ()

Browser.get ('http://www.yahoo.com')

Assert 'Yahoo' in broeser.title

Elem = browser.find_element_by_name ('p')

Elem.send_keys ('seleniumhq' + Keys.RETURN)

Browser.quit ()

3. Example 2:

Selenium webdriver is also often used for basic testing of web applications.

Import unittest

From selenium import webdriver

Class GoogleTestCase (unittest.TestCase):

Def setUp (self): self.browser = webdriver.Firefox () self.addCleanup (self.browser.quit) def testPageTitle (self): self.browser.get ('http://www.google.com') self.assertIn (' Google', self.browser.title))

If name = 'main':

Unittest.main (verbosity=2)

Tags: Examples browsers browsing programs tests drivers home pages variables fundamentals applications source code versions environments directories profiles web pages Yahoo online applications support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker vpn Microsoft Apple