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-折叠,判断折叠时可点击菜单

posted @ 2025-02-13 16:13  zmm521  阅读(26)  评论(0)    收藏  举报