Selenium Chrome驱动程序:如何修复我在尝试添加代理时遇到的错误?

klh5stk1  于 10个月前  发布在  其他
关注(0)|答案(1)|浏览(88)

我有以下代码,定义了Web3Driver类,我稍后会在代码中使用它。Web3Driver类似于常规的selenium driver对象,但具有特殊的属性和参数,如单独分配的代理,指纹等:

class Web3Driver:
    def __init__(self, wallet_id):
        self.wallet = db.get_wallet(wallet_id)
        self.mnemonic = self.wallet[2]
        possible_symbols = string.ascii_letters + string.digits
        combination_length = 15
        self.password = ''.join(random.choice(possible_symbols) for _ in range(combination_length))
        user_agent = self.wallet[13]
        chrome_options = Options()
        chrome_options.add_argument("--start-maximized")
        chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
        chrome_options.add_argument(f'user-agent={user_agent}')
        chrome_options.add_extension('10.33.0_0.crx')
        service = Service('chromedriver.exe')
        webdriver.DesiredCapabilities.CHROME['proxy'] = {
            "httpProxy": f'{self.wallet[12]}',
            "proxyType": 'MANUAL',
        }
        driver = webdriver.Chrome(service=service, options=chrome_options)
        self.driver = driver
        self.wait = WebDriverWait(self.driver, 10)
        logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s",
                            datefmt="%Y-%m-%d %H:%M:%S", filename=f"wallet_{wallet_id}.log")

字符串
它创建了web3driver对象,稍后将在我的代码中使用。当我第一次用web3driver = Web3Driver(wallet_id)初始化Web3Driver类的对象时,一切正常,初始化后,web3driver的方法也正常工作。但是在selenium驱动程序关闭后,稍后在我的程序中,我用web3driver = Web3Driver(wallet_id)启动了Web3Driver类的一个新对象。现在出现了一个例外

Exception in thread Thread-1 (manage_tasks):
Traceback (most recent call last):
  File "C:\Users\childoflogos\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\Users\childoflogos\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\manage_tasks.py", line 92, in manage_tasks
    tasks.bridge_to_zksync(task[0])
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\tasks.py", line 10, in bridge_to_zksync
    web3driver = Web3Driver(wallet_id)
                 ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\selenium_driver.py", line 45, in __init__
    driver = webdriver.Chrome(service=service, options=chrome_options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 49, in __init__
    super().__init__(
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 54, in __init__
    super().__init__(
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 206, in __init__
    self.start_session(capabilities)
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 291, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: proxy
from invalid argument: unrecognized proxy type: MANUAL
Stacktrace:
Backtrace:
    GetHandleVerifier [0x006AA813+48355]
    (No symbol) [0x0063C4B1]
    (No symbol) [0x00545358]
    (No symbol) [0x0055B42D]
    (No symbol) [0x005603E4]
    (No symbol) [0x0055A99B]
    (No symbol) [0x0058FCEE]
    (No symbol) [0x0058F9A8]
    (No symbol) [0x00590AD7]
    (No symbol) [0x0059093C]
    (No symbol) [0x0058A536]
    (No symbol) [0x005682DC]
    (No symbol) [0x005693DD]
    GetHandleVerifier [0x0090AABD+2539405]
    GetHandleVerifier [0x0094A78F+2800735]
    GetHandleVerifier [0x0094456C+2775612]
    GetHandleVerifier [0x007351E0+616112]
    (No symbol) [0x00645F8C]
    (No symbol) [0x00642328]
    (No symbol) [0x0064240B]
    (No symbol) [0x00634FF7]
    BaseThreadInitThunk [0x769800C9+25]
    RtlGetAppContainerNamedObjectPath [0x77927B4E+286]
    RtlGetAppContainerNamedObjectPath [0x77927B1E+238]


我向Selenium驱动程序功能添加代理的方式似乎是正确的(它确实毫无例外地工作,但只是第一次)。是什么原因导致了这个错误,我该如何修复它?

n3ipq98p

n3ipq98p1#

为“手动”代理设置指定所有必要字段,然后删除您可能不需要的字段,我怀疑可能需要的是"autodetect"

webdriver.DesiredCapabilities.CHROME['proxy'] = {
    "httpProxy": f'{self.wallet[12]}',
    "ftpProxy": f'{self.wallet[12]}',
    "sslProxy": f'{self.wallet[12]}',
    "noProxy": None,
    "proxyType": 'MANUAL',
    "autodetect": False
}

字符串

相关问题