python - 使用 Selenium : How to keep logged in after closing Driver in Python in whatsapp

标签 python selenium google-chrome selenium-chromedriver

我不想在 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 添加启动选项来保存 session -> 您需要添加选项 --user-data-dir <folder> .

我使用了这些地方的代码。

我在 Ubuntu 18.04 下运行此代码。

在运行此代码之前关闭 google-chrome。否则,selenium 将重新使用当前的浏览器实例,并且无法使用 --user-data-dir <folder> 运行它。 -选项。

#!/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)

关于python - 使用 Selenium : How to keep logged in after closing Driver in Python in whatsapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56681700/

相关文章:

python - 删除与模型关联的文件 - django

python - 获取 Django REST 中金额字段结果的总和

python - 反正弦的变体

python - 如何在 Flask 应用程序中创建动态子域

java - 我的 Selenium 测试应该在哪里检查预期/实际结果?

jquery - 在 chrome 错误中重新绘制跟踪

javascript - Chrome 扩展程序 : Detect copy action from Window's URL bar

python - 使用 python selenium : really slow execution 进行网页测试

java - 从下到上迭代元素列表的最佳方法是什么?

google-chrome - 隐藏控制台 chrome 中的错误?