Selenium4自动化测试1--Chrome浏览器和chromedriver

系列导航

一、Selenium4自动化测试1--Chrome浏览器和chromedriver

二、Selenium4自动化测试2--元素定位By.ID,By.CLASS_NAME,By.TAG_NAME

三、Selenium4自动化测试3--元素定位By.NAME,By.LINK_TEXT 和通过链接部分文本定位,By.PARTIAL_LINK_TEXT,css_selector定位,By.CSS_SELECTOR

四、jSelenium4自动化测试4--元素定位By.XPATH,元素定位最佳顺序

五、Selenium4自动化测试5--控件获取数据--ALERT弹窗、Confirm弹窗、Prompt弹窗

六、Selenium4自动化测试6--控件获取数据--下拉框级联选择、checkbox选择、时间选择器

七、Selenium4自动化测试7--控件获取数据--radio单选框、select下拉框选择、iframe

八、Selenium4自动化测试8--控件获取数据--上传、下载、https和切换分页

selenium的介绍

Selenium是一个用于Web应用程序测试的强大工具,可以模拟用户在浏览器中的行为。它提供了一组功能丰富的工具和库,使您能够自动化浏览器操作,进行功能测试、回归测试和跨浏览器测试。

Selenium原理

 它需要模拟浏览器的操作所以需要用到浏览器和浏览器驱动。

一、安装WebDriver

查看chrome版本号,设置-帮助-关于Google chrome,找到版本号

 

 

Chrome浏览器驱动(chromedriver )下载地址

1.普通版本,请在

https://registry.npmmirror.com/binary.html?path=chromedriver/

2 最新版本,均为test版本,请在

https://googlechromelabs.github.io/chrome-for-testing/

找不到相应的版本号,就重新安装chrom浏览器使版本匹配

将浏览器驱动放入,开发依赖的环境中查看驱动版本号:

D:\ProgramData\Anaconda3\envs\py38>chromedriver -version

ChromeDriver 123.0.6312.122 (31f8248cdd90acbac59f700b603fed0b5967ca50-refs/branch-heads/6312@{#824})

二、牛刀小试

打开B站并搜索老虎it学习并点击查询
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
# 定义一个driver的变量,用来接收实例化后的浏览器
driver = webdriver.Chrome()
# 使用get方法,访问网址
driver.get('https://www.bilibili.com/')
#1 找到输入框的位置,老虎资源分享
driver.find_element(By.CLASS_NAME,'nav-search-input').send_keys("老虎资源分享")
#2 找到搜索框的位置,点击搜索
driver.find_element(By.CLASS_NAME,'nav-search-btn').click()

time.sleep(3)

 这里如果谷歌浏览器和驱动版本不匹配总会出现报错,我们指定浏览器的位置,确保驱动和浏览器版本匹配。

import time

#pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
# 定义一个driver的变量,用来接收实例化后的浏览器
# 指定浏览器的位置,解决浏览器驱动和浏览器版本不匹配的问题
chrome_location = r'D:\pythonProject2023\SeleniumFirst\chrome-win64\chrome.exe'
options = webdriver.ChromeOptions()
options.binary_location = chrome_location
driver = webdriver.Chrome(options=options)

# 使用get方法,访问网址
driver.get('https://www.bilibili.com/')
#1 找到输入框的位置,输入老虎资源分享
driver.find_element(By.CLASS_NAME,'nav-search-input').send_keys("老虎资源分享")
#2 找到搜索框的位置,点击搜索
driver.find_element(By.CLASS_NAME,'nav-search-btn').click()

time.sleep(5)

 后续将持续更新,敬请期待

 

 

 

 

posted @ 2024-05-08 18:48  万笑佛  阅读(122)  评论(0编辑  收藏  举报