簡體   English   中英

使用 selenium:在 whatsapp 中關閉 Python 驅動程序后如何保持登錄狀態

[英]Using selenium: How to keep logged in after closing Driver in Python in whatsapp

我不想在https://web.whatsapp.com中一遍又一遍地登錄。 我嘗試了一些解決方案,但使用 selenium chrome 驅動程序不起作用。

options=Options
options.add_argument("user-data-dir=C:\\Users\\oyo\AppData\\Local\\Google\\Chrome\\User Data")
browser = webdriver.Chrome("chrome_options=options")

TypeError: add_argument() missing 1 required positional argument: 'argument'

我能夠通過向 chrome 添加啟動選項來保存會話 -> 您需要添加選項--user-data-dir <folder>

我用過這些地方的代碼。

我在 Ubuntu 18.04 下運行此代碼。

在運行此代碼之前關閉google-chrome。 否則 selenium 將重新使用當前瀏覽器實例,並且無法使用--user-data-dir <folder> -option 運行它。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

# Replace below path with the absolute path
# to chromedriver in your computer

options = webdriver.ChromeOptions();
options.add_argument("user-data-dir=~/.chrome_driver_session")
# Create the folder. Change path accordingly

driver = webdriver.Chrome('./chromedriver_78/chromedriver', chrome_options=options)
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)

# Replace 'Friend Name' with the name of your friend or the name of a group
target = '"Friend Name"'

# Replace the below string with your own message
# I'm unsure why it needs two empty spaces in front of it.

string = "  " + "Nachricht von Wichtel_Whatsapp"

x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located((By.XPATH, x_arg)))
group_title.click()

print("Clicked")

default_input = "Schreib eine Nachricht"
# Change the Text with the default of the input-field

inp_xpath = "//div[contains(.,'" + default_input + "')]"
input_box = wait.until(EC.presence_of_element_located((By.XPATH, inp_xpath))).find_element_by_xpath('..')
input_box.send_keys(string)
# If the Text is written in the input field, use this line:
# input_box.send_keys(string + Keys.ENTER)

為了將您的會話從一個瀏覽器實例轉移到另一個瀏覽器實例,您需要做的就是將Cookie從第一個會話復制到第二個。 Selenium 提供了多種允許 cookie 操作的方法,您將需要:

  1. driver.get_cookies() - 從您登錄的會話中獲取 cookie
  2. add_cookie() - 將 cookie 恢復到新的瀏覽器實例中

在您的情況下,您可以將 cookie 作為第一次執行的最后一步存儲到一個臨時文件中,並作為第二次執行的第一步從文件中讀取它們。

示例代碼:

#Store cookies
cookies = driver.get_cookies()
for cookie in cookies:
    with open('cookies.txt', 'a') as stored_cookies:
        stored_cookies.write(str(cookie) + '\n')

#Restore cookies
with open('cookies.txt') as stored_cookies:
    cookie = eval(stored_cookies.readline())
    driver.add_cookie(cookie)
import os
from selenium import webdriver

dir_path = os.getcwd()
profile = os.path.join(dir_path, "profile", "wpp")
options = webdriver.ChromeOptions()
options.add_argument(
        r"user-data-dir={}".format(profile))

browser = webdriver.Chrome("./chromedriver.exe", chrome_options=options)

browser.get("https://web.whatsapp.com")

我認為這是最好的方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM