简体   繁体   English

使用 selenium:在 whatsapp 中关闭 Python 驱动程序后如何保持登录状态

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

I don't want to log in over and over again in https://web.whatsapp.com .我不想在https://web.whatsapp.com中一遍又一遍地登录。 I've tried some solutions but it's not working using selenium chrome driver.我尝试了一些解决方案,但使用 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'

I was able to save the session by adding a start-option to chrome -> You need to add the option --user-data-dir <folder> .我能够通过向 chrome 添加启动选项来保存会话 -> 您需要添加选项--user-data-dir <folder>

I've used code from these places.我用过这些地方的代码。

I'm running this code under Ubuntu 18.04.我在 Ubuntu 18.04 下运行此代码。

Close google-chrome before runnning this code.在运行此代码之前关闭google-chrome。 Else selenium will re-use the current browser instance and won't be able to run it with the --user-data-dir <folder> -option.否则 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)

In order to transfer your session from one browser instance to another all you need to do is to copy Cookies from the first session to second.为了将您的会话从一个浏览器实例转移到另一个浏览器实例,您需要做的就是将Cookie从第一个会话复制到第二个。 Selenium provides a variety of methods allowing cookies manipulation , you will need: Selenium 提供了多种允许 cookie 操作的方法,您将需要:

  1. driver.get_cookies() - to fetch the cookies from the session where you're logged in driver.get_cookies() - 从您登录的会话中获取 cookie
  2. add_cookie() - to restore cookies into the new browser instance add_cookie() - 将 cookie 恢复到新的浏览器实例中

In your case you can store the cookies into an interim file as the last step of the first execution and read them from the file as the first step of the second execution.在您的情况下,您可以将 cookie 作为第一次执行的最后一步存储到一个临时文件中,并作为第二次执行的第一步从文件中读取它们。

Example code:示例代码:

#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")

I think this is the best way我认为这是最好的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用硒:如何在 Python 中关闭驱动程序后保持登录状态 - Using selenium: How to keep logged in after closing Driver in Python Python Selenium - 如何使用套件中的相同驱动程序实例清除用户登录 session - Python Selenium - How to clear user logged-in session using the same driver instance within the suite 如何从 web whatsapp 中提取此信息; 使用 python 和 selenium - How can extract this information from web whatsapp; using python and selenium 通过Python登录后如何使用Selenium点击Read the guide按钮 - How to click on the Read the guide button using Selenium after logged in through Python 使用 Selenium 和 Python 的 Whatsapp 自动化错误 - Error in Whatsapp Automation using Selenium and Python 在 Python 中使用 Selenium 关闭 Modal - Closing Modal using Selenium in Python 如何使用 Selenium Python 保存 Whatsapp web 的会话? - How to save the session of Whatsapp web with Selenium Python? 如何使用请求库(Python)保持登录网站? - How to keep logged in on a website using requests library (Python)? 在 python 中使用 IE selenium 驱动程序 - Using IE selenium driver with python 如何使用 selenium 抓取 Whatsapp web 的元素 - How to scrape elements of Whatsapp web using selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM