【发布时间】:2021-12-15 21:04:33
【问题描述】:
我正在抓取一些需要身份验证的带有 selenium 和代理的网站,因此我使用 this 扩展来设置代理的凭据,此时一切顺利。
def get_chromedriver(use_proxy=False, user_agent=None):
path = os.path.abspath(
os.path.join(os.path.dirname(__file__), './drivers/current/', 'chromedriver.exe'))
chrome_options = webdriver.ChromeOptions()
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
if user_agent:
chrome_options.add_argument('--user-agent=%s' % user_agent)
return driver
但是,我也需要设置以下选项
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--incognito")
整个函数看起来是这样的,但是当我执行下面的代码时没有设置代理,而是使用我的公共地址。我的代码有什么问题?
def get_chromedriver(use_proxy=False, user_agent=None):
path = os.path.abspath(
os.path.join(os.path.dirname(__file__), './drivers/current/', 'chromedriver.exe'))
chrome_options = webdriver.ChromeOptions()
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
chrome_options.add_extension(pluginfile)
if user_agent:
chrome_options.add_argument('--user-agent=%s' % user_agent)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(executable_path=path,
chrome_options=chrome_options)
return driver
【问题讨论】:
-
chrome_options = webdriver.ChromeOptions()从from selenium.webdriver.chrome.options import Options导入时应为chrome_options = Options()查看documentation -
@It_is_Chris 我只是绑它没有成功
-
你是怎么调用函数的?参数设置为
False和None -
是的,没错。我刚刚发现隐身参数正在禁用扩展!
标签: python selenium google-chrome-extension