使用硒:在whatsapp中关闭Python中的驱动程序后如何保持登录 [英] Using selenium: How to keep logged in after closing Driver in Python in whatsapp

查看:173
本文介绍了使用硒:在whatsapp中关闭Python中的驱动程序后如何保持登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想一遍又一遍地登录 https://web.whatsapp.com .我已经尝试了一些解决方案,但是使用硒铬驱动程序无法正常工作.

I don't want to log in over and over again in https://web.whatsapp.com. I've tried some solutions but it's not working using selenium chrome driver.

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>.

I was able to save the session by adding a start-option to chrome -> You need to add the option --user-data-dir <folder>.

我在这些地方使用过代码.

I've used code from these places.

  • Session Example in Java or maybe C# - https://stackoverflow.com/a/50637211/406423
  • Whatsapp in Python with Selenium - https://www.geeksforgeeks.org/whatsapp-using-python/

我正在Ubuntu 18.04下运行此代码.

I'm running this code under Ubuntu 18.04.

关闭 google-chrome,然后运行此代码.其他硒将重新使用当前的浏览器实例,并且无法使用--user-data-dir <folder> -option来运行它.

Close google-chrome before runnning this code. Else selenium will re-use the current browser instance and won't be able to run it with the --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)

这篇关于使用硒:在whatsapp中关闭Python中的驱动程序后如何保持登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆