python selenium
1、login
Driver放置路徑:Python38-32\Scripts\chromedriver.exe
chrom_options = webdriver.ChromeOptions()
# 最大化運行
chrom_options.add_argument(“--start-maximized”)
# 跳過“您的連接不是私密連接”
options.add_argument("--ignore-certificate-errors")
#無頭模式
options.add_argument("--headless")
options.add_argument("--no-sandbox") # 禁用沙箱
options.add_argument("--disable-gpu") # 禁用GPU加速
brower = webdeiver.Chrom(options=chrom_options)
brower,get(“https:xxx”)
2.基礎功能
查找元素:id(唯一性)、xpath、name、link、class
獲取文本(text),發送文本(send_keys(xxx))、點擊元素(click())、清除(clear())、
鼠標懸停(move_to_element(ele))、是否enable(is_enabled())、是否被選中(is_selected())
2.1.三種等待方式:
強制等待sleep(10)/隱式等待:無條件等待10s
1) 顯式等待:有條件等待10s
Import WebDriverWait
Import expected_conditions as EC
WebDriverWait(driver, 10,0.5).until(EC.visibity_of_element_located(ele))
2.2.瀏覽器彈出框
alart = driver.switch_to_alart()
alart.text
alart.accpet()
alart.dismiss()
2.3.下拉框
Import Select
sel = Selest(ele)
sel.select_by_index(0)
Sel.select_by_value(‘zz’)
2.4.文件上傳
<input type=”file”.....元素找到,send_keys(‘’)
2.5. 讀取table
selenium python 讀取table中tbody數據
from selenium.webdriver.common.by import By data = [] table = driver.find_element(By.XPATH, 'table xpath') tbody = table.find_element(By.TAG_NAME, "tbody") rows = tbody.find_elements(By.TAG_NAME, "tr") for row in rows: cells = row.find_elements(By.TAG_NAME, "td") row_data = [cell.text for cell in cells] data.append(row_data)
2.6 交互式元素的展開/折疊狀態
aria-expended 屬性,導航可折疊菜單中使用,true-展開,false-折疊,判斷折疊時可點擊菜單

浙公網安備 33010602011771號