selenium4+python3-使用总结

[TOC]

1
2
3
4
5
6
7
8
9
# 基本用法
driver.find_element(By.CLASS_NAME,"xx") # 好用 div里面的内容几乎全部靠它
driver.find_element(By.CSS_SELECTOR,"xx") # CSS 样式选择器 感觉不好用
driver.find_element(By.ID,"xx") # id 首推使用它
driver.find_element(By.LINK_TEXT,"xx") # 链接类型的首推
driver.find_element(By.NAME,"xx") #标签页太多
driver.find_element(By.PARITIAL_LINK_TEXT,"xx") #herf属性获取链接内容
driver.find_element(By.TAG_NAME,"xx") # 标签页太多
driver.find_element(By.XPATH,"xx") # 性能不是很好

环境准备

python版本: python3.8

系统: macOS 12.6

Selenium版本: selenium-4.7.2

python安装 brew install python@3.8

selenium 安装:

pip3 install selenium-4.7.2

pip3 install webdriver-manager # 浏览器驱动

反爬虫配置

1
2
3
4
5
6
7
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChainsoption = webdriver.ChromeOptions()

option.add_argument("--disable-blink-features=AutomationControlled")
option.add_experimental_option('exculdeSwitches', ['enable-automation'])
实例化浏览器对象

驱动下载地址 https://chromedriver.chromium.org/downloads 请于本机电脑浏览器版本一致

1
2
3
4
driver = Service(r"/opt/chromedriver")       #D:\\driver\\chromedriver #根据个人情况
browser = webdriver.Chrome(service=driver) #chrome浏览器
# driver = webdriver.Firefox() # 火狐浏览器
# driver = webdriver.Edge()
Class Name 定位

Class name 定位更容易一些

class=”ng-binding active” classs属性中的空格,表示此class属性有多个属性,且相互独立,使用class name定位此元素,只能是其中一个属性名 且唯一

属性 ng-binding active 都不是唯一 报错

例如

1
2
3
4
5
6
7
8
<a id="helo" class="ng-binding active" /a>
#错误写法
task_list = browser.find_element(By.CLASS_NAME, "ng-binding active")
# 报错 NoSuchElementException: Message: no such element: Unable to locate element
# 正确写法
# 目标是点击href标签 By.CLASS_NAME/CSS_SELECTOR不容易获取.需要用列表的方式获取。
time.sleep(3)
task_list = browser.find_element(By.PARTIAL_LINK_TEXT, "任务管理").click()
获取下拉列表(非select标签)

思路:获取所有的下拉值, 在选择自己需要的选项 通过find.elements获取所有class name 相同的值,在选择需要的选项。

class name 总计有7个 需要获取第二个。

1
2
3
4
5
6
7
8
9
10
# 用列表的方式将所有值存在数组中,
values = [browser.find_elements(By.CLASS_NAME, "ant-select-arrow")]
for value in values:
value[2].click() # 激活下拉列表 点击下拉按钮
print("点击下拉框的下拉按钮")
#点击故障标签内容 这种方式几乎可以取所有div类型的内容
ele_lists = [browser.find_elements(By.CLASS_NAME, 'ant-select-dropdown-menu-item')]
for ele in ele_lists:
print(ele[1].text) # 获取故障标签<li>的内容
error = ele[1].click() #点击故障下拉框的按钮

单个class name 获取title属性值

1
2
3
4
5
6
ele = browser.find_element(By.CSS_SELECTOR, "[class='ant-select-selection-selected-value']")
# ele = browser.find_element(By.CLASS_NAME, "ant-select-selection-selected-value")
print(print(ele.get_attribute("title"))
# xpath 路径可在网页复制 相对路径
# elx = browser.find_element(By.XPATH, '//*[@id="task-list-page"]/div/div[2]/div[2]/div/div[1]/div[1]/div[3]/div/div[1]/div/div/div')

鼠标操作
1
2
3
4
5
6
7
8
from selenium.webdriver import ActionChains

driver = Service(r"/opt/chromedriver")
browser = webdriver.Chrome(service=driver)
#创建一个鼠标操作的对象
action = ActionChains(browser)
action.move_to_element(error).click() # 鼠标点击error


selenium4+python3-使用总结
https://moreylee.github.io/2023/01/27/selenium4-python3-使用总结/
作者
morey
发布于
2023年1月27日
许可协议