如何使用 python selenium 在 Ubuntu Server 上通过代理自动化无头、未被检测到的 chrome 浏览器

faa*_*hmi 5 python proxy selenium google-chrome headless

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

当自动化启动时,我们会收到一个用于身份验证的代理警报“登录代理需要用户名和密码。您与此站点的连接不是私人的在此输入图像描述

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

问题是我想在 ubuntu 服务器上运行它,我必须使用 headless,但 pyautogui 不能在 headless 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:
Run Code Online (Sandbox Code Playgroud)

我已经尝试过这些解决方案,但找不到这三个解决方案的工作原理:

  • 未检测到的浏览器
  • 有代理
  • 无头奔跑

我已经尝试了很多东西,主要是在未检测到的 chromedriver 问题中共享的 ,但是当涉及到无头时,所有这些解决方案都会失败

使用 zip 扩展

使用 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)
Run Code Online (Sandbox Code Playgroud)

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

jac*_*blk -1

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

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

  • 我在帖子中写道,我已经使用了seleniumwire_options。问题是我正在使用未检测到的铬。它只是忽略传递到 selenium_options 的身份验证凭据,并继续提示用户名和密码。 (2认同)