selenium+python+Chrome的基本操作

这里写目录标题

  • [1. 对页面进行操作](#1. 对页面进行操作)
    • [1.1 打开网址](#1.1 打开网址)
    • [1.2 元素定位](#1.2 元素定位)
    • [1.3 鼠标操作](#1.3 鼠标操作)
    • [1.4 键盘操作](#1.4 键盘操作)
    • [1.5 下拉框处理](#1.5 下拉框处理)
      • [1.5.1 导入](#1.5.1 导入)
      • [1.5.2 选择](#1.5.2 选择)
      • [1.5.3 反选(deselect)](#1.5.3 反选(deselect))
    • [1.6 页面滚动](#1.6 页面滚动)
    • 1.7警告框处理

1. 对页面进行操作

1.1 打开网址

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.service import Service
#########################打开网址######################
#webdriver的路径
gecko_driver_path = r"D:\Installed_Software\Google_Chrome\Installtion_Position\Google\Chrome\Application\chromedriver.exe"
#固定搭配直接用就行了
service = Service(executable_path=gecko_driver_path)
# webdriver 获取浏览器的对象
driver = webdriver.Chrome(service=service)

# 准备一个网址https://www.baidu.com/
url = "https://www.baidu.com/"
# 打开浏览器
driver.get(url)
sleep(5)
# 回收资源
driver.quit()

1.2 元素定位

元素定位

python 复制代码
# 查找元素(标签、标记、元素)通过id
driver.find_element(by=By.ID, value='kw').send_keys("熊猫")
# 查找元素(标签、标记、元素)通过name
driver.find_element(by=By.NAME, value='wd').send_keys("熊猫")
查找元素(标签、标记、元素)通过class
driver.find_element(by=By.CLASS_NAME, value='s_ipt').send_keys("熊猫")
模拟鼠标操作
driver.find_element(by=By.ID, value='su').click()

a标签定位

python 复制代码
# 定位a标签 link text或partial linktext
driver.find_element(By.LINK_TEXT,"hao123").click()  # 法1
driver.find_element(by=By.LINK_TEXT, value="hao123").click()  # 法2
driver.find_element(by=By.PARTIAL_LINK_TEXT, value="hao").click()   # 法3

通过css选择器方式进行定位

python 复制代码
driver.find_element(by=By.CSS_SELECTOR, value="#kw").send_keys("熊猫")    # 法1
driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("熊猫") # 法2

通过xpath方式进行定位

python 复制代码
# xml 可扩展标记语言;html 超文本标记语言
driver.find_element(By.XPATH,"//*[@id='kw']").send_keys("熊猫")
sleep(3)
driver.find_element(By.ID, "kw").clear()    # 清除文本
driver.find_element(By.ID, "kw").send_keys("大熊猫")
driver.find_element(By.XPATH,"//*[@id='su']").click()

输出元素的属性

python 复制代码
print(driver.find_element(By.ID, "kw").size)
print(driver.find_element(By.ID, "kw").text)
print(driver.find_element(By.ID, "kw").is_enabled())
print(driver.find_element(By.ID, "kw").is_displayed())

页面等待(需要等待页面加载出来,代码执行速度快于页面的加载速度)

python 复制代码
# 强制等待
sleep(3)
# 显式等待   # 等待某个元素加载完成,每隔0.5s检查一次,最多等待5s
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID,"3")))
# 隐式等待
driver.implicitly_wait(5)

1.3 鼠标操作

python 复制代码
action = ActionChains(driver)
# action.context_click(driver.find_element(By.ID, "su"))    # 鼠标右击
action.move_to_element(driver.find_element(By.CLASS_NAME, "soutu-btn")) # 鼠标悬停
action.perform()    # 执行鼠标操作

1.4 键盘操作

python 复制代码
el = driver.find_element(By.ID, "kw")
el.send_keys("python")
sleep(1)
el.send_keys(Keys.CONTROL, "a")  # 全选 ctrl+a
sleep(1)
el.send_keys(Keys.BACKSPACE) # 删除
sleep(1)
el.send_keys("大熊猫")
sleep(1)
el.send_keys(Keys.CONTROL, "a")  # 全选 ctrl+a
sleep(1)
el.send_keys(Keys.CONTROL, "c")  # 全选 ctrl+c
sleep(1)
el.send_keys(Keys.CONTROL, "v")  # 全选 ctrl+v

1.5 下拉框处理

参考链接:selenium之下拉选择框Select

我们要进行试验的网站: http://sahitest.com/demo/selectTest.htm

1.5.1 导入

python 复制代码
from selenium.webdriver.support.select import Select

1.5.2 选择

Select类提供了三种选择某一选项的方法:

python 复制代码
select_by_index(index)
select_by_value(value)
select_by_visible_text(text)

针对于示例网站中的第一个select框:

python 复制代码
<select id="s1Id">
<option></option>
<option value="o1" id="id1">o1</option>
<option value="o2" id="id2">o2</option>
<option value="o3" id="id3">o3</option>
</select>

我们可以这样定位:

python 复制代码
from selenium import webdriverd
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
driver.get('http://sahitest.com/demo/selectTest.htm')

s1 = Select(driver.find_element_by_id('s1Id'))  # 实例化Select

s1.select_by_index(1)  # 选择第二项选项:o1
s1.select_by_value("o2")  # 选择value="o2"的项
s1.select_by_visible_text("o3")  # 选择text="o3"的值,即在下拉时我们可以看到的文本

driver.quit()

以上是三种选择下拉框的方式,注意:

  1. index从 0 开始
  2. value是option标签的一个属性值,并不是显示在下拉框中的值
  3. visible_text是在option标签中间的值,是显示在下拉框的值

1.5.3 反选(deselect)

Select提供了四个方法给我们取消原来的选择:

python 复制代码
deselect_by_index(index)
deselect_by_value(value)
deselect_by_visible_text(text)
deselect_all()

前三种分别于select相对应,第四种是全部取消选择,是的,你没看错,是全部取消。有一种特殊的select标签,即设置了multiple="multiple"属性的select,这种select框是可以多选的,你可以通过多次select,选择多项选项,而通过deselect_all()来将他们全部取消。

1.6 页面滚动

python 复制代码
#################################页面的滚动######################
js_str = "window.scrollBy(0,10000)"
driver.execute_script(js_str)
# 向下滚动500个像素
driver.execute_script('window.scrollBy(0,500)')
# 向上滚动500个像素
driver.execute_script('window.scrollBy(0,-500)')
# 向右滚动500个像素
driver.execute_script('window.scrollBy(500,0)')
# 向左滚动500个像素
driver.execute_script('window.scrollBy(-500,0)')

1.7警告框处理

python 复制代码
from selenium import webdriver
driver = webdriver.Chrome()
#触发alert弹窗
driver.find_element(By.ID, "1").click()
#获取警告框
al = driver.switch_to.alert
#获取警告框提示信息
alert_text = al.text
print("alert_text")
#接受确认警告框
al.accept()#点击确定会消除alert
相关推荐
三体世界11 小时前
测试用例全解析:从入门到精通(1)
linux·c语言·c++·python·功能测试·测试用例·测试覆盖率
Python私教11 小时前
Django全栈班v1.04 Python基础语法 20250912 下午
后端·python·django
xchenhao11 小时前
Scikit-Learn 对糖尿病数据集(回归任务)进行全面分析
python·机器学习·回归·数据集·scikit-learn·特征·svm
xchenhao11 小时前
Scikit-learn 对加州房价数据集(回归任务)进行全面分析
python·决策树·机器学习·回归·数据集·scikit-learn·knn
这里有鱼汤11 小时前
发现一个高性能回测框架,Python + Rust,比 backtrader 快 250 倍?小团队必备!
后端·python
☼←安于亥时→❦11 小时前
数据分析之Pandas入门小结
python·pandas
带娃的IT创业者11 小时前
《Python Web部署应知应会》No3:Flask网站的性能优化和实时监测深度实战
前端·python·flask
赴33511 小时前
图像拼接案例,抠图案例
人工智能·python·计算机视觉
TwoAI12 小时前
Scikit-learn 机器学习:构建、训练与评估预测模型
python·机器学习·scikit-learn
max50060012 小时前
OpenSTL PredRNNv2 模型复现与自定义数据集训练
开发语言·人工智能·python·深度学习·算法