selenium谷歌各种实用且少人知道的用法汇总

一、添加代理

options = ChromeOptions()
options.add_argument("--proxy-server={}".format('代理地址'))

二、加载插件

options = ChromeOptions()
extension_path = "插件路径"
options.add_argument(f"load-extension={extension_path}")

三、嵌套页面(切换iframe)

from selenium.webdriver.chrome.service import Service
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
service = Service('谷歌驱动路径')
options.add_argument('--no-sandbox')
browser = webdriver.Chrome(service=service, options=options)

四、selenium清除指纹过环境检测

Selenium自动化并过环境检测

五、添加cookie

from selenium.webdriver.chrome.service import Service
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
service = Service('谷歌驱动路径')
options.add_argument('--no-sandbox')
browser = webdriver.Chrome(service=service, options=options)
browser.execute_script('return window.localStorage.setItem("{}", "{}");'.format('cookie键', 'cookie值'))

六、嵌套shadow_root

前言:这是一种另类嵌套,需要一步步地进去才能获取里面的element

特征:

<settings-ui>
	#shadow-root
</settings-ui>

操作1:

shadow_parent_element = shadow_root.find_element(By.CSS_SELECTOR, 'settings-ui')
shadow_root = shadow_parent_element.shadow_root

操作2:

element = driver.execute_script('document.querySelector("settings-ui")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值