在不可检测的硒python中使用带有身份验证的代理

问题描述 投票:0回答:1

我已经在 python 中研究

undetectable-selenium
一段时间了,但我无法将它与具有身份验证的代理一起使用。 这是我尝试使用的代码,它适用于普通的硒(来自 selenium import webdriver),但不适用于
undetectable-selenium
:

import undetected_chromedriver as us
from selenium.webdriver.common.by import By
from time import sleep
import random
from selenium import webdriver

import os
import zipfile



PROXY_HOST = 'proxyip'  # rotating proxy or host
PROXY_PORT =  # port
PROXY_USER = 'user' # username
PROXY_PASS = 'pass' # password


manifest_json = """
{
    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "minimum_chrome_version":"22.0.0"
}
"""

background_js = """
var config = {
        mode: "fixed_servers",
        rules: {
        singleProxy: {
            scheme: "http",
            host: "%s",
            port: parseInt(%s)
        },
        bypassList: ["localhost"]
        }
    };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "%s",
            password: "%s"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
);
""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)


def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.dirname(os.path.abspath(__file__))
    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)
    driver = webdriver.Chrome('my dir',chrome_options=chrome_options)
    return driver

def main():

    driver = get_chromedriver(use_proxy=True)
    #driver.get('https://www.google.com/search?q=my+ip+address')
    driver.get('https://httpbin.org/ip')


if __name__ == '__main__':

    main()

如果我将 webdriver.Chrome() 替换为 us(unDetectable-selenium) us.Chrome() 它会显示此错误

  File "/Users/dismay/Desktop/file.py", line 97, in <module>
    driver = webdriver.Chrome('dir/chromedriver')
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/undetected_chromedriver/__init__.py", line 248, in __init__
    options._session = self
AttributeError: 'str' object has no attribute '_session'

我不知道如何解决

python selenium-webdriver webdriver undetected-chromedriver
1个回答
0
投票

SeleniumBase 有一个未检测到的 chromedriver 模式,可让您更改代理设置:

pip install seleniumbase
,然后用
python
运行:(设置代理字符串后)

from seleniumbase import SB

with SB(uc=True, proxy="USER:PASS@HOST:PORT") as sb:
    sb.open("https://nowsecure.nl/#relax")
    sb.assert_text("OH YEAH, you passed!", "h1")
    sb.post_message("Selenium wasn't detected!")

(它将

uc
设置为 True(以启用未检测到的 chromedriver 模式),并设置
proxy
设置。)

还有原始驱动程序模式:

from seleniumbase import DriverContext

with DriverContext(uc=True, proxy="USER:PASS@HOST:PORT") as driver:
    driver.get("https://nowsecure.nl/#relax")
© www.soinside.com 2019 - 2024. All rights reserved.