【问题标题】:In Python, how do I make Selenium work headless with a Saved Browser Session?在 Python 中,如何通过保存的浏览器会话使 Selenium 无头工作?
【发布时间】:2020-10-01 15:25:00
【问题描述】:

我正在尝试绕过 web.whatsapp.com 二维码扫描页面。这是我目前使用的代码:

options = webdriver.ChromeOptions();
options.add_argument('--user-data-dir=./User_Data')
driver = webdriver.Chrome(options=options)
driver.get('https://web.whatsapp.com/')

在第一次尝试时,我必须手动扫描 QR 码,而在以后的尝试中,它不会要求提供 QR 码。

但是,如果我在添加此行 chrome_options.add_argument("--headless") 后尝试执行相同操作,则会收到将 DevTools 活动端口写入文件时出错。我尝试了至少十几种不同的谷歌搜索解决方案,但都没有奏效。对此的任何帮助将不胜感激!谢谢。

到目前为止,尝试了一堆不同组合的不同参数,但没有任何效果:

options = Options() #decomment for local debugging
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--remote-debugging-port=9222')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')  # Last I checked this was necessary.
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--user-data-dir=./User_Data')
driver = webdriver.Chrome('chromedriver.exe', options=options)

driver.get('https://web.whatsapp.com/')

【问题讨论】:

    标签: python selenium whatsapp headless headless-browser


    【解决方案1】:

    最近我做了一个whatsapp bot,也遇到了同样的问题。找了半天想出了这个办法:

    第一个问题是浏览器缓存,如果它没有得到缓存在浏览器apdata中的二维码,它会一直等待扫描它。

    所以在我的程序中我使用了以下函数来获取:

    def apdata_path():
        path = str(pathlib.Path().absolute())
        driver_path = path + "\chromedriver.exe"
        apdata = os.getenv('APPDATA')
        apdata_path = "user-data-dir=" + \
        re.findall(".+.\Dta\D", a)[0] + \
        r'Local\Chromium\User Data\Default\Default'
        apdata_path = apdata_path.replace("\\", "\\"*2)
        return apdata_path
    

    在这里它找到第一个 apdata 路径 => C:\Users\AppData\ 然后我将路径的其余部分连接到缓存文件夹,在这种情况下我使用了 Chromium。在您的情况下,它将是:

    C:\Users\AppData\Local\Google\Chrome\User Data\Default

    可能有更好的方法来查找配置文件数据路径。找到它后,我设置了驱动程序:

    def chrome_driver(user_agent=0):
        usr_path = apdata_path()
        chrome_path = file_path() + '\Chromium 85\\bin\chrome.exe'
        options = webdriver.ChromeOptions()
        options.binary_location = r"{}".format(chrome_path)
        if user_agent != 0:
            options.add_argument('--headless')
            options.add_argument('--hide-scrollbars')
            options.add_argument('--disable-gpu')
            options.add_argument("--log-level=3")
            options.add_argument('--user-agent={}'.format(user_agent))
        options.add_argument(usr_path)
        driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)
        return driver
    

    这里我遇到了另一个问题,即有时 Selenium 无法工作,因为 Whatsapp 具有用户代理验证,以便能够验证浏览器版本是否兼容。我知道的不多,所以我通过反复试验得出了这个结论,也许这不是真正的解释。但它对我有用。

    因此,在我的 Bot 中,我创建了一个启动函数来获取用户代理并获取第一个 QR 扫描并将其保存在浏览器缓存中:

    def whatsapp_QR():
        driver = chrome_driver()
        user_agent = driver.execute_script("return navigator.userAgent;")
        driver.get("https://web.whatsapp.com/")
        print("Scan QR Code, And then Enter")
        input()
        print("Logged In")
        driver.close()
        return user_agent
    

    毕竟,我的机器人运行起来并不完美,但运行顺畅。我能够在无头模式下在我的测试组中发送消息。

    总而言之,在 apdata 中获取配置文件用户缓存以绕过 QR 码(但您需要在没有 headless 的情况下运行一次以创建第一个缓存)。

    然后获取用户代理以绕过 Wthatsapp 验证。所以整个选项集看起来像这样:

    options.add_argument('--headless')
    options.add_argument('--hide-scrollbars')
    options.add_argument('--disable-gpu')
    options.add_argument("--log-level=3")
    options.add_argument('--user-agent={}'.format(user_agent)) # User agent for validation
    options.add_argument(usr_path) #apdata user profile, to by pass QR code
    

    usr_path = "user-data-dir=rC:\Users\\AppData\Local\Google\Chrome\User Data\Default"

    【讨论】:

    • 谢谢!很好的答案。 :) 我终于在无头模式下使用了 Firefox,在无头模式下使用已保存的配置文件时,它比 Chrome 更简单。我的代码在 sendSWA.py 文件中:github.com/XtremePwnership/WhatsApp-Scheduler
    猜你喜欢
    • 2020-12-03
    • 1970-01-01
    • 2019-12-03
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多