【问题标题】:How to automate a headless, undetected chrome browser with proxy in python selenium如何在 python selenium 中使用代理自动化无头、未检测到的 chrome 浏览器
【发布时间】:2022-08-02 17:59:46
【问题描述】:

我正在自动执行 python selenium 上的任务以访问我无法访问的网站。此浏览器或应用程序可能不安全 尝试使用不同的浏览器。作为解决方案,我正在使用带有代理的未检测到的 chrome 浏览器。

当自动化开始时,我们会收到一个用于身份验证的代理警报 \"登录代理需要用户名和密码。您与此站点的连接不是私密的\"

为了解决这个问题,我使用了库 PyAutoGui。这样就填写了代理的用户名和密码,整个自动化就成功了。

问题是我想在无头上运行这个但 pyautogui 不适用于无头 chrome。它在屏幕上检测到一个打开的窗口并在那里抛出用户名和密码(在我的例子中是 PYCHARM 的代码脚本)。因此,由于代理未通过身份验证,因为它没有获得所需的值,因此页面不会加载,并且 selenium 自动化会由于预期的元素而引发错误。

File \"C:\\Users\\username\\PycharmProjects\\ProjectName\\venv\\lib\\site-packages\\selenium\\webdriver\\support\\wait.py\", line 90, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:

我已经尝试了这些解决方案,但找不到所有三个都在哪里工作:

  • 未检测到的浏览器
  • 带代理
  • 无头运行

我已经尝试了很多东西,主要是在undetected-chromedriver issues 中分享的,但是当涉及到无头时,所有这些解决方案都失败了

using extensions with zip

Using selenium wire proxy

selenium 似乎无法通过硒警报访问!

这是我的代码(在某些部分是伪代码)

import pandas as pd
from imap_tools import MailBox, AND
import re
# No Headless, Proxy, undetected
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from threading import Thread
import pyautogui
from selenium.webdriver.chrome.options import Options
import chromedriver_autoinstaller
import undetected_chromedriver as uc
from seleniumwire import webdriver
from seleniumwire import webdriver

# host_ip = \"xx.xxx.xx.xx\"
host_ip = \"x.xxx.xxx.xxx\"
port = \"xxxxx\"
proxy_username = \"xxxxxxxx\"
proxy_password = \"xxxxxxxx\"
ipCheckURL=\"http://whatismyip.com\"

URL=undetectedCheckUrl


def enter_proxy_auth(proxy_username, proxy_password):
    time.sleep(3)
    pyautogui.typewrite(proxy_username)
    pyautogui.press(\'tab\')
    time.sleep(5)
    pyautogui.typewrite(proxy_password)
    pyautogui.press(\'enter\')
    time.sleep(15)

def visitWebsiteAndAutomateAndReturnAValue(driver, url):

    driver.get(URL)
    #Do some Automation
    return Value



    chrome_options = Options()
    chrome_options.add_argument(\'--proxy-server={}\'.format(host_ip + \":\" + port))
    ucdriver = uc.Chrome(options=chrome_options, use_subprocess=True)
    x1 = Thread(target=visitWebsiteAndAutomateAndReturnAValue, args=(ucdriver, url))
    x2 = Thread(target=enter_proxy_auth, args=(proxy_username, proxy_password))
    x1.start()
    x2.start()
    value = x1.join()
    x2.join()
    print(value)

到目前为止,在类似的帖子中,我没有看到解决方案,但人们只是发布了他们关于网络安全或互联网如何运作的维基百科知识。 如果你知道我如何处理这个分享,我将非常感激。

  • 你遇到了什么错误?
  • 编辑了帖子。你可以检查一下。感谢您的回复。

标签: python selenium google-chrome proxy headless


【解决方案1】:

尝试使用seleniumwire,它支持带身份验证的代理。

from seleniumwire import webdriver
options = {
    'proxy': {
        'https': 'https://user:pass@192.168.10.100:8888',
    }
}
driver = webdriver.Chrome(seleniumwire_options=options)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2014-02-20
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多