• About
  • Advertise
  • Privacy & Policy
  • Contact
Tech News, Magazine & Review WordPress Theme 2017
  • Home
  • Computers
  • Games
  • Internet
  • Image
  • Top downloads
No Result
View All Result
  • Home
  • Computers
  • Games
  • Internet
  • Image
  • Top downloads
No Result
View All Result
Flickroom
No Result
View All Result
Home Others

2. Getting Started — Selenium Python Bindings 2 documentation

Share on FacebookShare on Twitter

Contents

  • 1 2. Getting Started¶
    • 1.1 2.1. Simple Usage¶
    • 1.2 2.2. Example Explained¶
    • 1.3 2.3. Using Selenium to write tests¶
    • 1.4 2.4. Flickroom of the example¶
    • 1.5 2.5. Using Selenium with remote WebDriver¶

2. Getting Started¶

2.1. Simple Usage¶

If you have installed Selenium Python bindings, you can start using it from Python like this .

 from 

Reading: 2. Getting Started — Selenium Python Bindings 2 documentation

selenium spell webdriver from selenium.webdriver.common.keys meaning Keys driver = webdriver. Firefox ( ) driver. get ( `` hypertext transfer protocol : //www.python.org '' ) insist `` python '' in driver. title elem = driver. find_element_by_name ( `` q '' ) elem. clear ( ) elem. send_keys ( `` pycon '' ) elem. send_keys ( Keys. return ) assert `` No results found. '' not in driver. page_source driver. close ( )

The above handwriting can be saved into a file ( eg : – python_org_search.py ), then it can be run like this :

 python  python_org_search. py

The python which you are running should have the selenium faculty installed .

2.2. Example Explained¶

The selenium.webdriver faculty provides all the WebDriver implementations. presently supported WebDriver implementations are Firefox, Chrome, IE and Remote. The Keys class provide keys in the keyboard like RETURN, F1, ALT etc .

 from  selenium  import  webdriver
 from  selenium.webdriver.common.keys  meaning  Keys

future, the case of Firefox WebDriver is created .

 driver  =  webdriver. Firefox ( )

The driver.get method acting will navigate to a page given by the URL. WebDriver will wait until the foliate has amply loaded ( that is, the “ onload ” event has fired ) before returning control to your test or script. Be mindful that if your page uses a bunch of AJAX on load then WebDriver may not know when it has wholly loaded :

 driver. get ( `` hypertext transfer protocol : //www.python.org '' )

The adjacent line is an affirmation to confirm that title has “ Python ” word in it :

 affirm  `` python ''  in  driver. style

WebDriver offers a number of ways to find elements using one of the find_element_by_* methods. For exemplar, the stimulation text element can be located by its identify assign using find_element_by_name method. A detail explanation of finding elements is available in the Locating Elements chapter :

 elem  =  driver. find_element_by_name ( `` q '' )

next, we are sending keys, this is similar to entering keys using your keyboard. particular keys can be sent using Keys class imported from selenium.webdriver.common.keys. To be safe, we ’ ll first clear any pre-populated textbook in the stimulation field ( e.g. “ Search ” ) so it doesn ’ deoxythymidine monophosphate affect our search results :

 elem. clear ( )
 elem. send_keys ( `` pycon '' )
 elem. send_keys ( Keys. return key )

After submission of the page, you should get the resultant role if there is any. To ensure that some results are found, make an assertion :

 affirm  `` No results found. ''  not  in  driver. page_source

finally, the browser window is closed. You can besides call leave office method acting rather of conclusion. The drop out will exit stallion browser whereas close will close one pill, but if just one pill was open, by nonpayment most browser will exit entirely. :

 driver. close ( )

2.3. Using Selenium to write tests¶

selenium is by and large used for writing test cases. The selenium box itself doesn ’ triiodothyronine provide a test tool/framework. You can write test cases using Python ’ s unittest module. The early options for a tool/framework are pytest and nozzle .
In this chapter, we use unittest as the model of choice. here is the change model which uses unittest module. This is a test for python.org search functionality :

 import  unittest
 from  selenium  consequence  webdriver
 from  selenium.webdriver.common.keys  meaning  Keys

 class  PythonOrgSearch ( unittest. TestCase ) :

     def  setup ( self ) :
         self. driver  =  webdriver. Firefox ( )

     def  test_search_in_python_org ( self ) :
         driver  =  self. driver
         driver. get ( `` hypertext transfer protocol : //www.python.org '' )
         self. assertIn ( `` python '',  driver. entitle )
         elem  =  driver. find_element_by_name ( `` q '' )
         elem. send_keys ( `` pycon '' )
         elem. send_keys ( Keys. return )
         affirm  `` No results found. ''  not  in  driver. page_source


     def  tearDown ( self ) :
         self. driver. close ( )

 if  __name__  ==  `` __main__ '' :
     unittest. main ( )

You can run the above test lawsuit from a shell like this :

 python  test_python_org_search. py
 .
 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 run  1  test  in  15.566 sulfur 

Read more: IP Subnet Calculator

o

The above result shows that the test has been successfully completed .
note : To run the above quiz in IPython or Jupyter, you should pass a pair of arguments to the main function as shown below :

 unittest. main ( argv = [ 'first-arg-is-ignored ' ],  exit = false )

2.4. Flickroom of the example¶

initially, all the basic modules required are imported. The unittest module is a built-in Python based on Java ’ s JUnit. This module provides the model for organizing the trial cases. The selenium.webdriver module provides all the WebDriver implementations. presently supported WebDriver implementations are Firefox, Chrome, IE and Remote. The Keys class provides keys in the keyboard like RETURN, F1, ALT etc .

 import  unittest
 from  selenium  import  webdriver
 from  selenium.webdriver.common.keys  significance  Keys

The test sheath class is inherited from unittest.TestCase. Inheriting from TestCase class is the manner to tell unittest module that this is a test casing :

 class  PythonOrgSearch ( unittest. TestCase ) :

The setup is part of low-level formatting, this method acting will get called earlier every quiz affair which you are going to write in this test case classify. here you are creating the case of Firefox WebDriver .

 def  apparatus ( self ) :
     self. driver  =  webdriver. Firefox ( )

This is the test sheath method. The examination case method acting should constantly start with characters test. The first line inside this method create a local citation to the driver object created in apparatus method .

 def  test_search_in_python_org ( self ) :
     driver  =  self. driver

The driver.get method acting will navigate to a foliate given by the URL. WebDriver will wait until the page has fully loaded ( that is, the “ onload ” event has fired ) before returning restraint to your test or script. Be aware that if your page uses a distribute of AJAX on load then WebDriver may not know when it has completely loaded :

 driver. get ( `` hypertext transfer protocol : //www.python.org '' )

The future credit line is an affirmation to confirm that claim has “ Python ” son in it :

 self. assertIn ( `` python '',  driver. title )

WebDriver offers a issue of ways to find elements using one of the find_element_by_* methods. For exercise, the input text element can be located by its name attribute using find_element_by_name method. detail explanation of finding elements is available in the Locating Elements chapter :

 elem  =  driver. find_element_by_name ( `` q '' )

adjacent, we are sending keys, this is exchangeable to entering keys using your keyboard. special keys can be send using Keys class imported from selenium.webdriver.common.keys :

 elem. send_keys ( `` pycon '' )
 elem. send_keys ( Keys. reelect )

After submission of the page, you should get the result as per search if there is any. To ensure that some results are found, make an affirmation :

 assert  `` No results found. ''  not  in  driver. page_source

The tearDown method acting will get called after every test method acting. This is a identify to do all cleanup actions. In the stream method acting, the browser window is closed. You can besides call leave office method acting alternatively of close. The depart will exit the entire browser, whereas close will close a yellow journalism, but if it is the only check opened, by default most browser will exit entirely. :

 def  tearDown ( self ) :
     self. driver. close up ( )

final lines are some kettle plate code to run the test suite :

 if  __name__  ==  `` __main__ '' :
     unittest. chief ( )

2.5. Using Selenium with remote WebDriver¶

To use the outside WebDriver, you should have Selenium waiter running. To run the waiter, use this command :

 java  - jar  selenium - server - standalone - 2. x. ten. jolt

While running the Selenium server, you could see a message looking like this :

 15 : 43 : 07.541  information  -  RemoteWebDriver  instances  should  connect  to :  hypertext transfer protocol : // 127.0.0.1 : 4444 / wd / hub

The above line says that you can use this URL for connecting to remote WebDriver. here are some examples :

 from  selenium  consequence  webdriver
 from  selenium.webdriver.common.desired_capabilities  spell  DesiredCapabilities

 driver  =  webdriver. Remote (
    command_executor = 'http : //127.0.0.1:4444/wd/hub ' ,
    desired_capabilities = DesiredCapabilities. chrome )

 driver  =  webdriver. Remote (
    command_executor = 'http : //127.0.0.1:4444/wd/hub ' ,
    desired_capabilities = DesiredCapabilities. opera )

 driver  =  webdriver. Remote (
    command_executor = 'http : //127.0.0.1:4444/wd/hub ' ,
    desired_capabilities = DesiredCapabilities. HTMLUNITWITHJS )

The desire capabilities is a dictionary, so alternatively of using the default dictionaries, you can specify the values explicitly :

 driver  =  webdriver. Remote (
    command_executor = 'http : //127.0.0.1:4444/wd/hub ' ,
    desired_capabilities = { 'browserName ' :  'htmlunit ' ,
                          'version ' :  '2 ' ,
                         'javascriptEnabled ' 

Read more: How to Use Google Forms

: dependable } )
generator : https://flickroom.net
Category : Web Browsers

Related Posts

Conversion of Prefix to Postfix expression – Flickroom

Google Issues Warning For 2 Billion Chrome Users

Conversion of Prefix to Postfix expression – Flickroom

Hidden Google: 10 Fun Search Tricks

Conversion of Prefix to Postfix expression – Flickroom

Here are the best new Safari extensions to download for iOS 15 and iPadOS 15 (Updated)

Conversion of Prefix to Postfix expression – Flickroom

How to Download Apple Safari on Computer and PC?

Conversion of Prefix to Postfix expression – Flickroom

Internet Download Manager for Chrome Extension 2022 (IDM)

Conversion of Prefix to Postfix expression – Flickroom

Google’s Help Documents Aren’t Always Up To Date

No Result
View All Result
Flickroom

Knowledge of science, technology and life

Follow Us

NEWS

  • Niobi
  • 2 Verified Hotel Reviews of Savis Hotel | https://flickroom.net
  • What is the WordPress .htaccess File?
  • How to install VPSSIM – A lightweight but simple control panel right on SSH
No Result
View All Result
  • Home

© 2021 Flickroom.net