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
相关推荐
weixin_419349791 分钟前
Python pdf转换为html
python·pdf
吉小雨12 分钟前
PyTorch经典模型
人工智能·pytorch·python
可愛小吉23 分钟前
Python 课程10-单元测试
开发语言·python·单元测试·tdd·unittest
student.J30 分钟前
傅里叶变换
python·算法·傅里叶
Freak嵌入式1 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
java·开发语言·数据结构·python·接口·抽象基类
crownyouyou1 小时前
最简单的一文安装Pytorch+CUDA
人工智能·pytorch·python
鸽芷咕1 小时前
【Python报错已解决】libpng warning: iccp: known incorrect sRGB profile
开发语言·python·机器学习·bug
WenGyyyL1 小时前
变脸大师:基于OpenCV与Dlib的人脸换脸技术实现
人工智能·python·opencv
laofashi20152 小时前
AirTest 基本操作范例和参数解释(一)
python·自动化·automation
XyLin.2 小时前
Msf之Python分离免杀
开发语言·python·网络安全·系统安全