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


猜你喜欢
- 本文实例讲述了Python socket套接字实现C/S模式远程命令执行功能。分享给大家供大家参考,具体如下:一. 前言要求: 使用pyth
- (1) 针对较为复杂的跨多表的数据业务级别的约束,可以通过触发器来替代大量的后台判断代码,效率较高且便捷。 (2) 如果想通过触发器辅助业务
- 1.直方图直方图: (1) 图像中不同像素等级出现的次数 (2) 图像中具有不同等级的像素关于总像素数目的比值。我们使用cv2.calcHi
- 全文索引在 MySQL 中是一个 FULLTEXT 类型索引。FULLTEXT 索引用于 MyISAM 表,可以在 CREATE TABLE
- 示例如下:function addFileToZip($path,$zip){ $handler=opendir($path);
- 本文主要解决两个问题,第一个,在element-ui中,直接设置参数排序,达不到预期效果,预期是按照数字的大小进行排序;第二个,想对表格中某
- 一、条件判断 if ( ) { } elsif ( ) {&nb
- Python基础环境搭建CENTOS 6.X 系列默认安装的 Python 2.6 ,目前开发中主要是使用 Python 2.7 ,这两个版
- maketrans()方法返回的字符串intab每个字符映射到字符的字符串outtab相同位置的转换表。然后这个表被传递到tra
- golang 最吸引人的地方可能就是并发了,无论代码的编写上,还是性能上面,golang 都有绝对的优势学习一个语言的并发特性,我喜欢实现一
- 这篇论坛文章(赛迪网技术社区)主要介绍了一种简单的MySQL数据库安装方法,详细内容请大家参考下文:虽然安装MySQL数据库的文章很多,但是
- 众所周知IE6不支持position:fixed,这个bug与IE6的双倍margin和不支持PNG透明等bug一样臭名昭著。前些天我做自己
- 【实用系列】-- 胖页面载入,加载JavaScript效果整理了一下代码,做了一些优化,算是最终版了。完全不需要对其他文件做任何修改,就是所
- # set 不支持索引和切片,是一个无需的不重复得到容器# 类似于字典,但是只有key 没有value# 创建集合dic1={}set1={
- Python中字符串前面我们经常看到加r(R)或u/(U)的前缀,而这两个符号是什么意思呢?1.r(R)r意为raw,表示不包含转义字符的原
- 本文实例讲述了Python使用logging模块实现打印log到指定文件的方法。分享给大家供大家参考,具体如下:可能我们经常会使用print
- 下面先给大家介绍下Python3 执行系统命令并获取实时回显最近在改造一些打包的逻辑,原来在 Windows 下是基于批处理制作的,由于批处
- EM算法实例通过实例可以快速了解EM算法的基本思想,具体推导请点文末链接。图a是让我们预热的,图b是EM算法的实例。这是一个抛硬币的例子,H
- JAVA正则表达式及字符串的替换与分解Java 提供了 java.util.regex 包来与正则表达式进行模式匹配java.util.re
- 实例1:获取星期字符串程序读入一个表示星期几的数字(1~7),输出对应的星期字符串名称。例如,输入 3,返回“星期三&a