[英]How do I save a whatsapp web session in selenium?
I am trying to acces whatsapp web with python without having to scan the QR code everytime I restart the program (because in my normal browser I also dont have to do that).我正在尝试使用 python 访问 whatsapp web,而不必每次重新启动程序时扫描 QR 码(因为在我的普通浏览器中,我也不必这样做)。 But how can I do that?
但是我该怎么做呢? Where is the data stored that tells whatsapp web to connect to my phone?
告诉 whatsapp web 连接到我的手机的数据存储在哪里? And how do I save this data and send it to the browser when I rerun the code?
当我重新运行代码时,如何保存这些数据并将其发送到浏览器?
I already tried this because someone told me I should save the cookies:我已经尝试过了,因为有人告诉我应该保存 cookie:
from selenium import webdriver
import time
browser = None
cookies = None
def init():
browser = webdriver.Firefox(executable_path=r"C:/Users/Pascal/Desktop/geckodriver.exe")
browser.get("https://web.whatsapp.com/")
time.sleep(5) # in this time I scanned the QR to see if there are cookies
cookies = browser.get_cookies()
print(len(cookies))
print(cookies)
init()
Unfortunately there were no cookies.. The output was 0 and [].不幸的是没有cookies..输出是0和[]。 How do I fix this probblem?
我该如何解决这个问题?
As mentioned in the answer to this question , pass your Chrome
profile to the Chromedriver
in order to avoid this problem.正如此问题的答案中所述,将您的
Chrome
配置文件传递给Chromedriver
以避免此问题。 You can do it like this:你可以这样做:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
driver = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", options=options)
This one works for me, I just created a folder, on the home directory of the script and a little modifications and it works perfectly.这个对我有用,我刚刚在脚本的主目录中创建了一个文件夹并进行了一些修改,它运行良好。
########### ############
E_PROFILE_PATH = "user-data-dir=C:\\Users\\Denoh\\Documents\\Project\\WhatBOts\\SessionSaver" E_PROFILE_PATH = "user-data-dir=C:\\Users\\Denoh\\Documents\\Project\\WhatBOts\\SessionSaver"
################## This is the Config File that I will import later ################## ##################这是我稍后将导入的配置文件##################
The main script starts here ##################主脚本从这里开始##################
from selenium import webdriver from config import E_PROFILE_PATH from selenium import webdriver from config import E_PROFILE_PATH
options = webdriver.ChromeOptions() options.add_argument(E_PROFILE_PATH) options = webdriver.ChromeOptions() options.add_argument(E_PROFILE_PATH)
driver = webdriver.Chrome(executable_path='chromedriver_win32_86.0.4240.22\\chromedriver.exe', options=options) driver.get('https://web.whatsapp.com/') driver = webdriver.Chrome(executable_path='chromedriver_win32_86.0.4240.22\\chromedriver.exe', options=options) driver.get('https://web.whatsapp.com/')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.