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

klh5stk1  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(264)

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

  1. class Web3Driver:
  2. def __init__(self, wallet_id):
  3. self.wallet = db.get_wallet(wallet_id)
  4. self.mnemonic = self.wallet[2]
  5. possible_symbols = string.ascii_letters + string.digits
  6. combination_length = 15
  7. self.password = ''.join(random.choice(possible_symbols) for _ in range(combination_length))
  8. user_agent = self.wallet[13]
  9. chrome_options = Options()
  10. chrome_options.add_argument("--start-maximized")
  11. chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
  12. chrome_options.add_argument(f'user-agent={user_agent}')
  13. chrome_options.add_extension('10.33.0_0.crx')
  14. service = Service('chromedriver.exe')
  15. webdriver.DesiredCapabilities.CHROME['proxy'] = {
  16. "httpProxy": f'{self.wallet[12]}',
  17. "proxyType": 'MANUAL',
  18. }
  19. driver = webdriver.Chrome(service=service, options=chrome_options)
  20. self.driver = driver
  21. self.wait = WebDriverWait(self.driver, 10)
  22. logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s",
  23. 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类的一个新对象。现在出现了一个例外

  1. Exception in thread Thread-1 (manage_tasks):
  2. Traceback (most recent call last):
  3. File "C:\Users\childoflogos\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
  4. self.run()
  5. File "C:\Users\childoflogos\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
  6. self._target(*self._args, **self._kwargs)
  7. File "C:\Users\childoflogos\Desktop\airdrop_hunter\manage_tasks.py", line 92, in manage_tasks
  8. tasks.bridge_to_zksync(task[0])
  9. File "C:\Users\childoflogos\Desktop\airdrop_hunter\tasks.py", line 10, in bridge_to_zksync
  10. web3driver = Web3Driver(wallet_id)
  11. ^^^^^^^^^^^^^^^^^^^^^
  12. File "C:\Users\childoflogos\Desktop\airdrop_hunter\selenium_driver.py", line 45, in __init__
  13. driver = webdriver.Chrome(service=service, options=chrome_options)
  14. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  15. File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 49, in __init__
  16. super().__init__(
  17. File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 54, in __init__
  18. super().__init__(
  19. File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 206, in __init__
  20. self.start_session(capabilities)
  21. File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 291, in start_session
  22. response = self.execute(Command.NEW_SESSION, caps)["value"]
  23. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  24. File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute
  25. self.error_handler.check_response(response)
  26. File "C:\Users\childoflogos\Desktop\airdrop_hunter\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
  27. raise exception_class(message, screen, stacktrace)
  28. selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: proxy
  29. from invalid argument: unrecognized proxy type: MANUAL
  30. Stacktrace:
  31. Backtrace:
  32. GetHandleVerifier [0x006AA813+48355]
  33. (No symbol) [0x0063C4B1]
  34. (No symbol) [0x00545358]
  35. (No symbol) [0x0055B42D]
  36. (No symbol) [0x005603E4]
  37. (No symbol) [0x0055A99B]
  38. (No symbol) [0x0058FCEE]
  39. (No symbol) [0x0058F9A8]
  40. (No symbol) [0x00590AD7]
  41. (No symbol) [0x0059093C]
  42. (No symbol) [0x0058A536]
  43. (No symbol) [0x005682DC]
  44. (No symbol) [0x005693DD]
  45. GetHandleVerifier [0x0090AABD+2539405]
  46. GetHandleVerifier [0x0094A78F+2800735]
  47. GetHandleVerifier [0x0094456C+2775612]
  48. GetHandleVerifier [0x007351E0+616112]
  49. (No symbol) [0x00645F8C]
  50. (No symbol) [0x00642328]
  51. (No symbol) [0x0064240B]
  52. (No symbol) [0x00634FF7]
  53. BaseThreadInitThunk [0x769800C9+25]
  54. RtlGetAppContainerNamedObjectPath [0x77927B4E+286]
  55. RtlGetAppContainerNamedObjectPath [0x77927B1E+238]


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

n3ipq98p

n3ipq98p1#

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

  1. webdriver.DesiredCapabilities.CHROME['proxy'] = {
  2. "httpProxy": f'{self.wallet[12]}',
  3. "ftpProxy": f'{self.wallet[12]}',
  4. "sslProxy": f'{self.wallet[12]}',
  5. "noProxy": None,
  6. "proxyType": 'MANUAL',
  7. "autodetect": False
  8. }

字符串

相关问题