Python automation: summary of common methods in selenium

The Python version used is 3.8 and the selenium version is 4.15.2

Please add image description

Python automation: summary of common methods in selenium

1. Three waiting methods
  1. Force waiting
    Use moduletimeThe sleep() below implements the waiting effect
  2. Tsushiki Tomatai
    Usedriver.implicitly_wait() method is global. After it is set in the front of the code, it will be valid during the entire program running and will wait for the page to be loaded. There is no need to set it every time after the execution.
    Disadvantages: The entire page must be loaded before executing the code, which affects the execution efficiency of the code.
    Reference code:
from selenium import webdriver

driver = webdriver.Chrome()
driver.get(url='')
driver.implicitly_wait(5)
  1. Display waiting
    It must be declared in front of each element that needs to be waited for, making it clear that it must wait until a certain element appears or is clickable. Need to use: WebDriverWait and expected_conditions
    Reference Code:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

ele = WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located(By.,''))
2. Browser operation

driver.maximize_window() # Maximum screen
driver.minimize_window() # Minimum screen
driver.quit() # Exit the browser a>
driver.close() # Close the current page
driver.set_window_size(width=,height=) # Set the size of the browser
driver.set_window_position(x=,y=) # Set the position of the browser displayed on the computer
driver.back() # Simulate the browser back button
driver .forward() # Simulate browser forward button
driver.refresh() # Refresh the current page
driver.title # Title of the current page< a i=10> driver.current_url # URL of the current page

3. 8 ways to find elements
  1. By.XPATH general search method
    Get Baidu page input box
driver.find_element(By.XPATH,"//input[@id='kw']").send_keys('哈哈')
  1. By.CLASS_NAME Search by class attribute
driver.find_element(By.CLASS_NAME,"s_ipt").send_keys('哈哈')
  1. By.ID Find by id attribute
driver.find_element(By.ID,"kw").send_keys('哈哈')
  1. By.CSS_SELECTOR Use the css selector. If you can write css styles, you will know
driver.find_element(By.CSS_SELECTOR,"#kw").send_keys('哈哈')
  1. By.NAME Search by name attribute
driver.find_element(By.NAME,"wd").send_keys('哈哈')
  1. By.LINK_TEXT By link text
    Find the Tieba hyperlink on the Baidu page and enter
driver.find_element(By.LINK_TEXT,"贴吧").click()
  1. By.PARTIAL_LINK_TEXT partial link text
driver.find_element(By.PARTIAL_LINK_TEXT,"贴").click()
  1. By.TAG_NAME Pass the tag element
4. Advanced events
  1. Browser popup
# 浏览器弹框
    driver.switch_to.alert
        .accept()  确认
        .dismiss() 取消
  1. frame page
driver.switch_to.frame()

The reason why this is used is that some pages may not be just one page, but several pages may be combined into one page through frame or iframe tag elements. What you need to pay attention to when searching for elements is that if the element you are looking for is not under the current page, you need to find the corresponding page, and then use the above method to jump to the corresponding page, and then you can find the corresponding element.
Please add image description

  1. Window switching
objs = driver.window_handles
# 获取当前的窗口对象 列表
driver.switch_to.window(objs[1])
# 下标从0开始

4. Slider verification

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()
driver.get(url='file:///D:/Sublime/vscode/Test/%E6%BB%91%E5%9D%97%E6%B5%8B%E8%AF%95/%E6%BB%91%E5%8A%A8%E9%AA%8C%E8%AF%81.html')
driver.maximize_window()
ele = driver.find_element(By.XPATH,"//div[@class='slider']")
# 滑块元素
ac = ActionChains(driver)
ac.click_and_hold(ele).move_by_offset(300,0).perform()
# 鼠标点击 握住 向右移动300像素
ac.release()
# 释放

Run result:
Insert image description here
[Note]: There are many advanced operations, here is just a summary of the most basic ones.

Guess you like

Origin blog.csdn.net/qq_45404396/article/details/134921758