ChromeDriver下载页面:
配置环境变量,加入path,C:\Program Files\Google\Chrome\Application
win+r cmd
输入验证是否安装成功chromedriver
1、百度浏览器,实现自动搜索周杰伦
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
import time
#实例化service,将chromedriver.exe
#双斜杠
s = Service('C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe')
driver = webdriver.Chrome(service=s)
#url
url = "https://www.baidu.com"
driver.get(url)
#输入
input_baidu = driver.find_element("id", "kw")
#输入框输入周杰伦
input_baidu.send_keys("周杰伦")
#模仿键盘回车
input_baidu.send_keys(Keys.RETURN)
#停留3s
time.sleep(3)
#关闭
driver.quit()
2、使用pytest,不使用allure生成测试报告
---将代码放入test_开头的函数内,文件名也需要是test_ 开头命名
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
import time
def test_selenium():
# 实例化service,将chromedriver.exe
s = Service('C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe')
driver = webdriver.Chrome(service=s)
# 设置等待时间
driver.implicitly_wait(10)
# url
url = "https://www.baidu.com"
driver.get(url)
# 输入
input_baidu = driver.find_element("id", "kw")
input_baidu.send_keys("周杰伦")
input_baidu.send_keys(Keys.RETURN)
time.sleep(3)
# 关闭
driver.quit()
---终端进入该目录下,pytest执行一下
---安装pytest --html
---pytest --html=report.html 执行生成一个名为report的html文件,里头包含测试下来的结果