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

时间:2019-06-20 08:09:28

标签: python selenium google-chrome selenium-chromedriver

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

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'

3 个答案:

答案 0 :(得分:1)

> rleid(c(1, 1, 2, 3, 3), c("a", "b", "b", "d", "d"))
[1] 1 2 3 4 4

我认为这是最好的方法

答案 1 :(得分:0)

要将会话从一个浏览器实例转移到另一个浏览器实例,您要做的就是将Cookies从第一个会话复制到第二个会话。 Selenium provides a variety of methods allowing cookies manipulation,您将需要:

  1. driver.get_cookies()-从您登录的会话中获取Cookie
  2. add_cookie()-将Cookie还原到新的浏览器实例中

在您的情况下,您可以将cookie存储到临时文件中,作为第一次执行的最后一步,并将cookie从文件中读取,作为第二次执行的第一步。

示例代码:

  let $container = $("#container");
  let colours = ["56, 68, 97", "97, 56, 80", "42, 74, 53", "104, 66, 44"];

  (function colourAnimation() {
    colours.forEach((colour) => {
      $container.animate({"color": "rgb(" + colour + ")",
                          "background-color": "rgba(" + colour + ", 0.2)",
                          "border-color": "rgba(" + colour + ", 0.7)",
                        }, 2500);
    });
    $container.animate({}, 0, "", colourAnimation);
  })()

答案 2 :(得分:0)

我能够通过向chrome添加开始选项来保存会话->您需要添加选项--user-data-dir <folder>

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

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

在运行此代码之前,请

关闭 google-chrome。其他硒将重新使用当前的浏览器实例,并且无法使用--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)