SoFunction
Updated on 2024-11-19

The most complete python library selenium automation use detailed tutorials

I. Installing selenium

pip install Selenium

II. Initializing the browser

  • Chrome is initialized Google Chrome
  • Firefox is the initialized Firefox browser
  • Edge is the initialization of Internet Explorer
  • PhantomJS is an interface-less browser.
from selenium import webdriver
 
driver = ()

Third, set the browser size

maximize_window maximize_window

set_window_size Customize window size

from selenium import webdriver
 
driver = ()
driver.maximize_window()

IV. Access page

from selenium import webdriver
 
driver = ()
 
('')

V. Positioning elements

The basic method of positioning an element is as follows

Positioning an element Positioning multiple elements account for
find_element_by_id find_elements_by_id Positioning by element id
find_element_by_name find_elements_by_name Positioning by element name
find_element_by_xpath find_elements_by_xpath Locate by xpath expression
find_element_by_link_text find_elements_by_link_tex Locate by full hyperlink
find_element_by_partial_link_text find_elements_by_partial_link_text Locate by partial link
find_element_by_tag_name find_elements_by_tag_name Positioning by tag
find_element_by_class_name find_elements_by_class_name Localization by class name
find_elements_by_css_selector find_elements_by_css_selector Positioning via css selector

Demonstration: Finding the input box on Baidu's homepage

from selenium import webdriver
 
driver = ()
('')
driver.find_element_by_id('kw')

VI. Another way to write positioning elements

Need to introduce By module

from selenium import webdriver
from  import By
 
driver = ()
('')
driver.find_element(, 'kw')

VII. Element Interaction

methodologies

account for

click() Click on an element
send_keys(input value) analog input
clear() Clearing operations
submit() Submit form
get_attribute(name) Getting the value of an element's attribute
location Getting the position of an element
text Get the text value of the element
size Get the size of the element
id Get the id value of the element
tag_name Get the tag name of the element

Demo: Type I am autofelix in the Baidu input box and click the search button.

from selenium import webdriver
 
driver = ()
('')
driver.find_element_by_id('kw').send_keys('I'm autofelix'.)
driver.find_element_by_id('su').click()

VIII. Implementation of js

from selenium import webdriver
 
driver = ()
driver.maximize_window()
 
('')
 
js_sql = '''
  ('kw').value = 'I'm autofelix'.
'''
driver.execute_script(js_sql)

IX. Frame operation

  • If you have a frame on the page, you need to perform a cut-in/cut-out operation.
  • switch_to.from(id name of the child iframe) cut to
  • switch_to.parent_frame(name of the id of the parent iframe) cutout
from selenium import webdriver
 
driver = ()
driver.maximize_window()
 
('')
 
//this site does not have iframe, I guess there is, you guys look at it on the line
driver.switch_to.frame('The iframe I guessed up')

X. Cookie operation

 

methodologies clarification
delete_all_cookies() Delete all cookies from the current page
get_cookie(name) Get the value of the specified cookie
get_cookies() Get all cookies for the current page
add_cookie() Setting cookie values

XI. Tab management

methodologies account for
window_handles Save tuple of all tabs
switch_to.window() Toggle tabs
from selenium import webdriver
 
driver = ()
driver.maximize_window()
 
('')
 
driver.delete_all_cookies()
driver.add_cookie({'name': 'name', 'domain': '.', 'value': 'autofelix'})

XII. Mouse events

Mouse events need to be introducedActionChains module

methodologies clarification
move_to_element(above) right click
double_click() double-click
drag_and_drop() Left click and hold to drag
perform() Motion Storage

Demo: Sliding Slider Captcha

from selenium import webdriver
from  import ActionChains
from  import By
from  import WebDriverWait
from  import expected_conditions as EC
 
# Initialize Google Chrome
driver = ()
 
# Maximize the window
driver.maximize_window()
 
# Open the headline landing site
('')
 
# Waiting to see if an element is present
WebDriverWait(, 10).until(
  EC.text_to_be_present_in_element((, '//*[@]/span'), u'send')
)
 
# Instantiate mouse operations
action = ActionChains()
 
# Press and hold the slider
action.click_and_hold(.find_element_by_xpath('//*[@]')).perform()
 
# Move the slider x distance
action.move_by_offset(xoffset=x, yoffset=0).perform()
 
# Release the slider
().perform()

XIII. Waiting

  • Cryptic waiting
  • If the specified element is not present by a certain time, the process does not block, but if it is not found by the specified time, an exception is thrown
from selenium import webdriver
 
driver = ()
driver.implicitly_wait(10)
 
('')
  • Show Waiting
  • If the specified element does not appear within a certain period of time, the process will block here, and if it is not found by the specified time, an exception will be thrown
from selenium import webdriver
from  import By
from  import WebDriverWait
from  import expected_conditions as EC
 
driver = ()
driver.implicitly_wait(10)
 
('')
WebDriverWait(driver, 10).until(
  EC.presence_of_element_located((, 'kw'))
)

XIV. Forward, backward and refresh

  • back
  • forward
  • refresh Refreshes the browser
from selenium import webdriver
driver = ()
('')
('')
('')
 
()
()
()

XV. Closing the browser

  • close Closes the current tab
  • quit Close the entire browser
from selenium import webdriver
driver = ()
('')
//Open Baidu page, close the entire browser
()

To this point this article on the most complete python library selenium automation tutorials on the use of this article, more related python library selenium automation content please search for my previous articles or continue to browse the following articles I hope you will support me in the future more!