selenium设置浏览器为headless无头模式(Chrome和Firefox)
作者:哦... 发布时间:2023-02-19 14:09:08
标签:selenium,无头模式
新版本的selenium已经明确警告将不支持PhantomJS,建议使用headless的Chrome或FireFox。
两者使用方式非常类似,基本步骤为:
下载驱动
创建选项,设定headless
创建WebDriver,指定驱动位置和选项
对URL发起请求,获得结果,进行解析
Chrome
驱动的下载路径为:https://chromedriver.storage.googleapis.com/index.html
接下来创建选项并设定headless:
options = webdriver.ChromeOptions()
options.set_headless()
创建WebDriver,指定驱动位置和选项:
driver = webdriver.Chrome(
'D://chromedriver_win32//chromedriver', chrome_options=options)
发起请求,获得结果并进行解析:
driver.get('http://www.sohu.com/')
time.sleep(3)
print(driver.page_source)
driver.close()
Firefox
驱动的下载路径为:https://github.com/mozilla/geckodriver
启动的步骤与Chrome一致,只不过使用的选项对象和创建的WebDriver对象略有不同。直接上源代码:
options = webdriver.FirefoxOptions()
options.set_headless()
driver = webdriver.Firefox(
firefox_options=options,
executable_path='D:/geckodriver-win64/geckodriver')
driver.get('http://www.sohu.com/')
time.sleep(3)
print(driver.page_source)
driver.close()
SELENIUM使用HEADLESS无头模式实现 * 面运行
先导包:
from selenium.webdriver.chrome.options import Options
加入如下配置:
chrome_options = Options()
chrome_options.add_argument('--window-size=1920,1080') # 设置窗口界面大小
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
参考代码:
from selenium import webdriver
import time
import multiprocessing
from selenium.webdriver.chrome.options import Options
class Zutuan():
def __init__(self):
"""打开浏览器"""
self.chrome_options = Options()
self.chrome_options.add_argument('--window-size=1920,1080')
self.chrome_options.add_argument('--headless')
self.driver = webdriver.Chrome(chrome_options=self.chrome_options)
def open_zutuan(self, url):
"""传入组团url"""
self.driver.get(url)
#self.driver.maximize_window()
self.driver.refresh()
#time.sleep(0.01)
self.driver.implicitly_wait(30) # todo implicitly隐式等待,等待元素可见
def option_element(self, user, password):
"""xpath定位元素"""
self.driver.find_element_by_xpath('//div[@class="login a"]/i').click()
time.sleep(0.01)
self.driver.find_element_by_xpath('//div[@class="a-title"]').click()
self.driver.find_element_by_xpath('//input[@type="text" or @class="userName"]').send_keys(user)
self.driver.find_element_by_xpath('//input[@type="password"]').send_keys(password)
self.driver.find_element_by_xpath('//div[@class="button"]').click()
time.sleep(1)
self.driver.refresh()
def select_commodity(self, content):
"""搜索组团商品"""
# TODO self.content实例属性传给下面的方法使用,如果想把值给下面的方法用,添加实例属性解决
self.content = content
self.driver.find_element_by_xpath('//input[@type="text"]').send_keys(content)
self.driver.find_element_by_xpath('//div[@class="search"]').click()
self.driver.refresh()
#return content
def result(self):
"""判断搜索商品成功后的提示信息,断言页面是否成功"""
if self.content in self.driver.page_source:
#print(self.content)
print('商品搜索成功,测试通过')
else:
print('商品搜索错误,测试失败')
def closed(self):
"""关闭浏览器"""
time.sleep(1)
self.driver.quit()
def run1():
# TODO 根据操作顺序,调用方法执行
zt = Zutuan()
zt.open_zutuan('http://www.zutuan.cn/index.html#/')
zt.option_element('1489088761@qq.com', 'mg123456')
zt.select_commodity('香蕉')
zt.result()
zt.closed()
class View_details(Zutuan):
"""把商品添加为明星单品,"""
def check_commodity(self, number):
"""进入商品详情页,点击添加明星单品"""
self.driver.find_element_by_xpath('//a[@target="_blank"]/img').click()
self.driver.switch_to.window(self.driver.window_handles[1])
self.driver.find_element_by_xpath('//div[@class="child start"]').click()
self.driver.find_element_by_xpath('//div[@class="el-dialog__body"]//input[@type="text"]').send_keys(number)
self.driver.find_element_by_xpath('//button[@type="button" and @class="el-button el-button--danger"]').click()
time.sleep(1)
def result(self):
"""重写父类方法,判断商品添加成功后的提示信息,断言页面是否成功"""
if '添加成功' in self.driver.page_source:
print('商品添加成功,测试通过')
else:
print('商品添加失败,测试失败')
# 调用父类方法关闭
super().closed()
def run2():
vd = View_details()
vd.open_zutuan('http://www.zutuan.cn/index.html#/')
vd.option_element('1489088761@qq.com', 'mg123456')
vd.select_commodity('苹果')
vd.check_commodity(91628)
vd.result()
def main():
p1 = multiprocessing.Process(target=run1)
p2 = multiprocessing.Process(target=run2)
p1.start()
p2.start()
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/piglite/article/details/86317485
0
投稿
猜你喜欢
- 核心代码:from random import randomfrom time import timefrom PyQt5.QtCore i
- 导入模块import numpy as npimport pandas as pd1.读取测试数据data=pd.read_csv(r
- 今天,在完成一个小的python习题,习题的主要内容是读取一个帮助模块,并保存到本地文件。知道是用pydoc进行模块的读取,但是在windo
- 1,file_put_contents()函数 2,使用PHP内置缓存机制实现页面静态化:output_bufferingphp中outpu
- IE 浏览器中 CSS Expression 特性的最大的问题:会反复执行,每秒钟可能执行了成百上千次,有严重的性能问题。如何对 CSS E
- 带参数的二维码对于渠道营销推广来说是很有用的,可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送,可喜的是微信开通了这个
- 框架特色:一、统一命名空间 默认命名空间为F,当然你也可以改成自己喜欢的名字,整个框架
- 步骤如下: 1.使用torchvision加载并预处理CIFAR-10数据集、2.定义网络3.定义损失函数和优化器4.训练网络并更新网络参数
- 装饰器的基础使用(装饰带参函数)def decorator(func): def inner(info): &nb
- 本文实例讲述了Python多线程编程之多线程加锁操作。分享给大家供大家参考,具体如下:Python语言本身是支持多线程的,不像PHP语言。下
- 要点说明在绘制散点图的时候,通常使用变量作为输入数据的载体。其实,也可以使用字符串作为输入数据的存储载体。下面代码的data = {“a”:
- 测试环境Python 3.6Win10代码实现#!/usr/bin/env python 3.4.0#-*- encoding:utf-8
- 如下所示:aa = [1,2,3,4,5]aa.index(max(aa))如果aa是numpy数组:aa = numpy.array([1
- EXISTS该函数返回集合中第一个元素的索引,如果集合为空,返回NULLNULLNULLCollection.EXISTS(index)CO
- 通过界面设计上是能手工操作的,无法达到我批量修改几千台服务器。 因为此了一个脚本来批量执行。 环境:redgate + mssql 2008
- 本文实例讲述了Python实现对excel文件列表值进行统计的方法。分享给大家供大家参考。具体如下:#!/usr/bin/env pytho
- np.r_是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等。np.c_是按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等。具体见示
- 简介由于项目在注册、登录、找回密码 时需要发送短信验证的功能,我们使用腾讯云短信做。为什么要用腾讯云短信呢? 因为注册就送 100条免费短信
- 首字母为英文和下划线,其它部分则可以是英文、数字和下划线(即:_),而变量名称是区分大小写,即变量temp与Temp为不同变量。变量的基本用
- 在实际处理数据时,因系统内存有限,我们不可能一次把所有数据都导出进行操作,所以需要批量导出依次操作。为了加快运行,我们会采用多线程的方法进行