在Selenium中使用扩展(Python) [英] Using Extensions with Selenium (Python)

查看:201
本文介绍了在Selenium中使用扩展(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用Selenium来运行Chrome的实例来测试网页。每次脚本运行时,都会启动一个干净的Chrome实例(清除扩展名,书签,浏览历史记录等)。我想知道是否可以使用Chrome扩展程序运行我的脚本。我试过寻找一个Python例子,但是当我搜索这个例子的时候没有出现。 你应该使用chrome webdriver < options 来设置一个列表加载的扩展。下面是一个例子:

 从selenium导入os 
从selenium.webdriver.chrome.options导入webdriver
import选项


executable_path =path_to_webdriver
os.environ [webdriver.chrome.driver] = executable_path

chrome_options = Options()
chrome_options.add_extension('path_to_extension')

driver = webdriver.Chrome(executable_path = executable_path,chrome_options = chrome_options)
driver.get(http://stackoverflow.com )
driver.quit()

希望有帮助。


I am currently using Selenium to run instances of Chrome to test web pages. Each time my script runs, a clean instance of Chrome starts up (clean of extensions, bookmarks, browsing history, etc). I was wondering if it's possible to run my script with Chrome extensions. I've tried searching for a Python example, but nothing came up when I googled this.

解决方案

You should use chrome webdriver options to set a list of extensions to load. Here's an example:

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


executable_path = "path_to_webdriver"
os.environ["webdriver.chrome.driver"] = executable_path

chrome_options = Options()
chrome_options.add_extension('path_to_extension')

driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()

Hope that helps.

这篇关于在Selenium中使用扩展(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆