Selenium Common Operations

Keywords: Python Selenium Windows JSON Attribute

Catalog

install

pip install selenium



Element Selection

Single element lookup method

  • Note: elementplus an S is the way to find the corresponding multiple element s

  • Find by xpath
    • find_element_by_xpath()
  • Find by id
    • find_element_by_id()
  • Find by class attribute
    • find_element_by_class_name()
  • Find through the css selector ( Rule of grammar )
    • find_element_by_css_selector()
  • Find by Link Text
    • find_element_by_link_text()
  • Find by partial matching of linked text
    • find_element_by_partial_ink_text()
  • Find by name attribute
    • find_element_by_name()
  • Find by tag name
    • find_element_by_tag_name()

Both methods integrate all of the above search methods

find_element(By.ID, '...')

  • Find one by ID

find_elements(By.XPATH, '...')

  • Find multiple through Xpath

By.xxx Choose the lookup method to use and parse it as XXX

  • By object import: from selenium.webdriver.common.by import By





Enter, clear, Click Example Back to Catalog

  • Input Content
    • send_keys('Content')
  • Clear Content
    • clear()
  • click
    • click()



Close windows, browsers Example Back to Catalog

  • Close the current handle window (do not close the process)
    • close()
  • Close entire browser process
    • quit()



Window Switch Example Back to Catalog

The selenium operation is the first open window, the newly opened window cannot be directly operated on

Handle of window needs to be switched

  • Get handles to all windows
    • window_handles
      • The order of window_handles is not the order of windows on the browser
  • Get a handle to the current window
    • current_window_handle
  • Switch handle to specified window
    • switch_to.window( window_handles[1] )



Frame Switching Example Back to Catalog

The iframe tag usually appears on the login page

  • Cut to specified frame, locate with id, name, element
    • switch_to.frame()
  • Cutting to the parent frame, if it is already the main document, has no effect and is equivalent to going back
    • driver.switch_to.parent_frame()
  • Switch to the main page, the first frame of the DOM tree
    • driver.switch_to_default_content()



Page Pop-up Window Example Back to Catalog

  • Locate the alert pop-up window and return the object of a pop-up window
    • switch_to.alert
  • Canceling a pop-up object (equivalent to clicking the Cancel button on a pop-up window)
    • dismiss()
  • Determine the object of the pop-up window (equivalent to clicking the OK button on the pop-up window)
    • accept()
  • Enter data into the input box within the pop-up window object (for prompt pop-ups)
    • send_keys(key)
  • Get the text inside the pop-up window
    • text



Waiting to load Example Back to Catalog

Import WebDriverWait

  • from selenium.webdriver.support.wait import WebDriverWait

Import expected_conditions

  • from selenium.webdriver.support import expected_conditions as EC

Explicit Wait

  • Specify a condition, and then set the maximum wait time to check at regular intervals (default 0.5 seconds)
  • If the condition is true, proceed to the next step, or wait until the maximum time set is exceeded, then throw the timeout exception: TimeoutException
  • WebDriverWait(driver, timeout, poll_frequency,igonred_exceptions).until(method, message)
    • driver:Instance of WebDriver
    • timeout: Maximum time to wait
    • poll_frequency: The interval between calls to methods in until, default 0.5 seconds
    • igonred_exceptions: Ignored exception, if an exception in this tuple is thrown during a call to until, the code will not be interrupted and the wait will continue

Waiting conditions: expected_conditions

  • These two conditions verify whether an element appears, and the parameters passed in are tuple-type locator s, such as by.ID,'kw', which pass as long as one qualifying element is loaded, and the other must have all qualifying elements loaded.
    • presence_of_element_located(This is used most often)
    • presence_of_all_elements_located
  • These two condition classes validate the title, verifying that the parameter title passed in is equal to or contained in the driver
    • title_is
    • title_contains
  • These three conditions verify whether the element is visible, the first two incoming parameters are tuple-type locator s, and the third one is WebElement
    • visibility_of_element_located
    • invisibility_of_element_located
    • visibility_of
  • To determine whether a piece of text appears in an element, one to determine the text of an element, and one to determine the value of an element
    • text_to_be_present_in_element
    • text_to_be_present_in_element_value
  • Determine if frame can be cut in, either by passing in a locator tuple or by directly passing in a location: id, name, index, or WebElement
    • frame_to_be_available_and_switch_to_it
  • Determine if alert appears
    • alert_is_present
  • Determine if an element is clickable, pass in locator
    • element_to_be_clickable

Implicit Wait

  • Set a maximum wait time, and if the page loads within the specified time, proceed to the next step, otherwise wait until the time is up, and then proceed to the next step
  • Recessive wait time works for the entire driver cycle, so the default wait time is 0 if you set it once
  • driver.implicitly_wait(10) #Implicit wait, up to 10s
  • malpractice
    • The program waits for the entire page to load, even if the elements you need are loaded
    • That is, you normally see the small circle in the browser tab bar no longer turning before you proceed to the next step

Recessive waiting and dominant waiting can be used at the same time, the longest waiting time is the largest of both.




Action Chain Example Back to Catalog

Import ActionChains: from selenium.webdriver.common.action_chains import ActionChains

  • Perform all actions in the chain
    • perform()
  • Click and hold the left mouse button
    • click_and_hold(on_element=None)
  • Release the left mouse button at an element location
    • release(on_element=None)
  • Move the mouse to the specified x,y position
    • move_by_offset(xoffset, yoffset)
  • Move the mouse to how far away from an element
    • move_to_element_with_offset(to_element, xoffset, yoffset)
  • Drag onto an element and release
    • drag_and_drop(source, target)
  • Drag to a coordinate and release
    • drag_and_drop_by_offset(source, xoffset, yoffset)
  • Mouse moves to an element
    • move_to_element(to_element)
  • Double-click left mouse button
    • double_click(on_element=None)
  • Right-click mouse
    • context_click(on_element=None)
  • Left mouse click on the incoming element
    • click(on_element=None)




Keyboard events Example Back to Catalog

Keys object import: from selenium.webdriver.common.keys import Keys

  • Delete key (BackSpace)
    • send_keys(Keys.BACKSPACE)
  • Spacebar
    • send_keys(Keys.SPACE)
  • Tab key
    • send_keys(Keys.TAB)
  • Back key (Esc)
    • send_keys(Keys.ESCAPE)
  • Enter
    • send_keys(Keys.ENTER)
  • Select All (Ctrl+A)
    • send_keys(Keys.CONTROL,'a')
  • Copy (Ctrl+C)
    • send_keys(Keys.CONTROL,'c')
  • Cut (Ctrl+X)
    • send_keys(Keys.CONTROL,'x')
  • Paste (Ctrl+V)
    • send_keys(Keys.CONTROL,'v')
  • Keyboard F1
    • send_keys(Keys.F1)
  • Keyboard F12
    • send_keys(Keys.F12)



Execute js Example Back to Catalog

  • Execute_script (js statement)



screenshot Example Back to Catalog

  • get_screenshot_as_file('***.png')



Get Element Properties Example Back to Catalog

  • Get properties on element labels
    • get_attribute('href')
  • Coordinates of the element
    • location
  • Size of element
    • size



Get page source code, refresh page Example Back to Catalog

  • Page Source Code
    • page_source
  • Refresh
    • refresh()



Set window size Example Back to Catalog

  • minimize
    • minimize_window()
  • Maximize
    • maximize_window()
  • Set the specific size of the window
    • set_window_size(width,height)
  • Set window position
    • set_window_position(x,y)



Set up proxy, UA Example Back to Catalog

  • Set up proxy
    • webdriver.ChromeOptions().add_argument('--proxy-server=http://IP:Port')
  • Set up UA
    • webdriver.ChromeOptions().add_argument('User-Agent=ua')
  • No spaces on either side of'='



Cookie operation Example Back to Catalog

  • Get Cookie s
    • get_cookies()
  • Add Cookie s
    • add_cookie()
    • You need to open any page before adding cookie s
    • Otherwise an error will be reported: InvalidCookieDomainException: Message: invalid cookie domain
  • Delete Cookie
    • delete_all_cookies()
  • Cookie Key Name
    • name:Name of cookie
    • Value:cookie corresponding value
    • Domain: server domain name
    • expiry:cookie valid end date
    • The path:path property defines which paths on the Web server pages can get cookie s set by the server
    • httpOnly: Anti-script attack
    • secure: specifies that cookie s can only be sent to the server under the https protocol



Skip Selenium detection Example Back to Catalog

window.navigator.webdriver

  • Normal browser, in js, window.navigator.webdriver is undefined
  • Selenium, js, window.navigator.webdriver is True
  • Use
    • webdriver.ChromeOptions().add_experimental_option('excludeSwitches', ['enable-automation'])



Example

Execute js, switch windows, close windows, close browsers Back to Catalog

import time
from selenium import webdriver

driver = webdriver.Chrome()
try:
    driver.get("http://news.baidu.com/")
    print('implement js Before opening a new window')
    print('Handles to all windows:', driver.window_handles)
    print('Handle to current window:', driver.current_window_handle)
    # Execute js to open a new window
    new_js = 'window.open("https://www.toutiao.com/")'
    driver.execute_script(new_js)
    print('implement js After opening a new window')
    all_handle = driver.window_handles
    print('Handles to all windows:', all_handle)
    print('Handle to current window:', driver.current_window_handle)
    print('Switch Handle...')
    driver.switch_to.window(all_handle[1])
    print('Handle to current window:', driver.current_window_handle)

    # Page Height
    height = 0
    # Execute js sliding scrollbar
    while height < 10000:
        # Slide the scrollbar to the specified position
        driver.execute_script('document.documentElement.scrollTop=10000')
        # Page Height
        height = driver.execute_script('return document.body.scrollHeight')
        
    time.sleep(2)
    # Close the newly opened window
    driver.close()
finally:
    time.sleep(3)
    # Close Browser
    driver.quit()




Log in to QQ space: frame switch, enter content, clear content, click, keyboard events Back to Catalog

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
try:
    driver.get("https://qzone.qq.com/")
    time.sleep(0.5)
    """Cut to specified by element positioning frame(Login Box)
    frame = driver.find_element(By.XPATH,'//iframe')
    driver.switch_to.frame(frame)
    """
    # Frames can also be located by ID or name
    driver.switch_to.frame('login_frame')
    # Click on account password to log in
    driver.find_element(By.LINK_TEXT, 'Account Password Logon').click()
    time.sleep(0.5)
    text_box = driver.find_element(By.ID, 'u')
    # Input Content
    text_box.send_keys('0123456789')
    time.sleep(1)
    # Clear Content
    text_box.clear()
    time.sleep(1)

    text_box.send_keys('Correct Account')
    password_box = driver.find_element(By.CSS_SELECTOR, '.inputstyle.password')
    password_box.send_keys('Correct password')
    time.sleep(1.5)

    # Perform keyboard operations: Enter key
    password_box.send_keys(Keys.ENTER)
    time.sleep(7)

finally:
    time.sleep(3)
    # Close Browser
    driver.quit()



Page pop-ups, screenshots, getting element properties Back to Catalog

import time
from selenium import webdriver

driver = webdriver.Chrome()
try:
    driver.get("https://www.baidu.com/")
    time.sleep(0.3)
    driver.find_element_by_link_text('Set up').click()
    time.sleep(0.3)
    driver.find_element_by_link_text('Search Settings').click()
    time.sleep(0.3)
    driver.find_element_by_link_text('Save Settings').click()
    time.sleep(1)
    #   Get the object of the popup window
    alert_box = driver.switch_to.alert
    #   Get the contents of a pop-up window
    print('Pop-up window content:',alert_box.text)
    #   Determine operation on pop-up window object
    alert_box.accept()

    #   Button: Baidu once
    button = driver.find_element_by_css_selector('[type="submit"]')
    #   Get properties on element labels
    print('get_attribute: ', button.get_attribute('value'))
    #   Coordinates of the element
    print('location: ', button.location)
    #   Size of element
    print('size: ', button.size)

    #   screenshot
    driver.get_screenshot_as_file('1.png')


finally:
    time.sleep(3)
    # Close Browser
    driver.quit()



Set window size, get page source code, refresh page Back to Catalog

import time
from selenium import webdriver

driver = webdriver.Chrome()
try:
    driver.get("https://www.toutiao.com/")
    time.sleep(1)
    # minimize
    driver.minimize_window()
    time.sleep(1)
    # Set the specific size of the window
    driver.set_window_size(500,500)
    time.sleep(1)
    # Set window position
    driver.set_window_position(800,200)
    time.sleep(1)
    # Maximize
    driver.maximize_window()
    time.sleep(1)
    # Print Web Page Source (JS Rendered)
    print(driver.page_source)
    # Refresh
    driver.refresh()

finally:
    time.sleep(3)
    # Close Browser
    driver.quit()



Cookie operation Back to Catalog

import time
import json
from selenium import webdriver

driver = webdriver.Chrome()

try:
    driver.get('https://www.cnblogs.com/')

    """Obtain Cookie
    # Wait 60 seconds for manual login
    time.sleep(60)
    # Save the obtained cookies
    cookies = driver.get_cookies()
    
    with open('1.txt', 'w+') as f:
        f.write(json.dumps(cookies))
    """



    """Add to Cookie
    
    with open('1.txt') as f:
        cookies = json.loads(f.read())

    # You need to open any page before adding cookie s
    # Then add it
    # Otherwise an error will be reported: InvalidCookieDomainException: Message: invalid cookie domain
    for cookie in cookies:
        driver.add_cookie(cookie)
    # Once you've added it and opened the corresponding page, you're logged in
    driver.get('https://www.cnblogs.com/')
    time.sleep(5)
    """



    """empty cookie
    driver.delete_all_cookies()
    # Unlogged after clearing cookie s
    driver.get('https://www.cnblogs.com/')
    """

finally:
    time.sleep(3)
    driver.quit()



Set up proxy, UA Back to Catalog

import time
from selenium import webdriver

opt = webdriver.ChromeOptions()
#   Set up proxy
opt.add_argument('--proxy-server=http://121.40.162.239:808')
#   Set up UA
opt.add_argument('User-Agent=ABCDEFG')

driver = webdriver.Chrome(options=opt)

try:
    driver.get('http://httpbin.org/get')

finally:
    time.sleep(3)
    driver.quit()



Waiting to load Back to Catalog

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
try:
    driver.get('https://www.python.org/getit/')
    
    # Explicit Wait Criteria --> Arguments are array types
    method = EC.presence_of_element_located((By.LINK_TEXT, 'Downloads'))
    # Explicit Wait
    WebDriverWait(driver, 20, 0.2).until(method)
    # Implicit Wait
    # driver.implicitly_wait(20)
    
    driver.find_element(By.LINK_TEXT, 'Downloads').click()

finally:
    time.sleep(10)
    driver.quit()



Action Chain Back to Catalog

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
try:
    driver.get('http://www.treejs.cn/v3/demo/cn/exedit/drag.html')
    time.sleep(2)
    
    # Locate Elements
    element = driver.find_element_by_id('treeDemo_2_span')
    target = driver.find_element_by_id('treeDemo_12_span')

    #   Action Chain: Drag
    ActionChains(driver).drag_and_drop(element,target).perform()

finally:
    time.sleep(10)
    driver.quit()



Skip Selenium detection Back to Catalog

import time
from selenium import webdriver

opt = webdriver.ChromeOptions()
opt.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(options=opt)
try:
    driver.get('http://www.baidu.com')
finally:
    time.sleep(10)
    driver.quit()




Posted by johanafm on Wed, 08 Jan 2020 19:47:31 -0800