selenium 确实是好东西,使用selenium-server 加快执行速度,对速度有很大提升,同时可以拆分服务,进行集群部署。

前言


本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/116972995

未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,关于selenium-server


本来本地执行selenium 也可以,但是使用 selenium-server 会更快速,同时也更稳定。

https://www.selenium.dev/selenium/docs/api/py/index.html

2,本地模式


from selenium import webdriver
from selenium.webdriver.common.keys import Keys



options = webdriver.ChromeOptions()

desired_capabilities = options.to_capabilities()  # 将功能添加到options中
desired_capabilities['loggingPrefs'] = {
    "performance": "ALL"  # 添加日志
}


# 手机模式
mobile_emulation = {"deviceName": "iPhone 6"}
options.add_experimental_option("mobileEmulation", mobile_emulation)
# 开发者模式
options.add_argument("--auto-open-devtools-for-tabs")

options.add_argument('--no-sandbox')
# 无头模式,不打开浏览器
options.add_argument('headless')

with webdriver.Chrome(chrome_options=options,desired_capabilities=desired_capabilities) as driver:


	driver.get("https://m.bilibili.com/channel/223")

	print(driver.title)
	elem = driver.find_element_by_class_name("search")
	print(elem)

	source = driver.page_source
	# print(source)

	print(driver.get_log('browser'))
	print(driver.get_log('driver'))
	#print(driver.get_log('client'))
	#print(driver.get_log('server'))

	for entry in driver.get_log('browser'):
		params = json.loads(entry.get('message')).get('message')
		print(params.get('request'))  # 请求连接 包含错误连接
		print(params.get('response'))  # 响应连接 正确有返回值得连接
 

3,使用文档selenium-server


下载 jar:
http://selenium-release.storage.googleapis.com/4.0/selenium-server-standalone-4.0.0.jar

https://www.selenium.dev/selenium/docs/api/py/index.html
然后执行:
java -jar selenium-server-standalone-4.0.0.jar

再使用 remote 的api 即可:

#from selenium import webdriver

import selenium.webdriver.remote.webdriver

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


options = selenium.webdriver.ChromeOptions()

desired_capabilities = options.to_capabilities()  # 将功能添加到options中
desired_capabilities['loggingPrefs'] = {
    "performance": "ALL" # 添加日志
}


# 手机模式
mobile_emulation = {"deviceName": "iPhone 6"}
options.add_experimental_option("mobileEmulation", mobile_emulation)
# 开发者模式
options.add_argument("--auto-open-devtools-for-tabs")

options.add_argument('--no-sandbox')
# 无头模式,不打开浏览器
options.add_argument('headless')

with selenium.webdriver.remote.webdriver.WebDriver(options=options,
	command_executor="http://127.0.0.1:4444/wd/hub",desired_capabilities=DesiredCapabilities.CHROME) as driver:

#with webdriver.Chrome(chrome_options=options,desired_capabilities=desired_capabilities) as driver:

	# https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html
	# 
	driver.get("https://m.bilibili.com/channel/223")

	print(driver.title)
	elem = driver.find_element_by_class_name("search")
	print(elem)

	source = driver.page_source
	# print(source)
	print(driver.get_log('browser'))
	print(driver.get_log('driver'))

	print(driver.get_log('server'))


都是访问同一个网站:

使用 time 命令对比下:

本地模式:
real 0m2.505s
user 0m0.575s
sys 0m0.124s

远程模式:
real 0m1.551s
user 0m0.126s
sys 0m0.026s

可以看出来,1.5 s 对比 2.5s 还是快了不少呢,要是并行执行,频繁的创建 chrome 服务,消耗还是挺大的。

4,总结


selenium server 非常方便,可以直接远程访问,方便进行服务拆分,微服务化。

本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/116972995

博主地址是:https://blog.csdn.net/freewebsys

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 要加快Selenium速度,可以采取以下几种方法。首先,可以禁用加载策略,将加载策略设置为"none",这样可以跳过页面加载的等待时间。其次,可以使用retry机制进行重试,以防止报错,这样可以提高执行的稳定性。另外,可以设置隐式等待时间来等待元素的出现,但相比retry机制,retry更为稳妥。最后,可以使用无头浏览器来执行操作,这样可以避免浏览器界面的显示,提高执行速度。综上所述,通过禁用加载策略、使用retry机制、设置隐式等待时间和使用无头浏览器,可以加快Selenium速度。\[2\]\[3\] #### 引用[.reference_title] - *1* [[Selenium]你真的知道怎么提高Selenium的运行速度吗?](https://blog.csdn.net/m0_72760466/article/details/129758259)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Selenium运行慢 - 通过设置selenium加载策略加快运行速度](https://blog.csdn.net/weixin_43831559/article/details/125898986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值