【问题标题】:Selenium Cookie Retrieving WarningsSelenium Cookie 检索警告
【发布时间】:2022-02-16 15:47:02
【问题描述】:

我正在使用 selenium 和 idle 来创建和重用 selenium sicne 的 cookie,每次登录到某个网站都很麻烦。

我用来保存cookie的示例代码:-

from webserver import keep_alive
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import pickle

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
driver.get("https://discord.com/login")
time.sleep(120)
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

在我能够将 cookie 保存在文件中之后,我编辑了代码以在下次执行时检索 cookie

from webserver import keep_alive
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import pickle

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
driver.get("https://discord.com/login")
cookies = pickle.load(open("cookies.pkl", "rb"))
time.sleep(5)
for cookie in cookies: 
  driver.add_cookie(cookie)
driver.get("https://discord.com/login")

但它没有按预期工作,在尝试插入 cookie 时出现错误:-

main.py:15: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  username_input = driver.find_element_by_name('email')
main.py:19: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  password_input = driver.find_element_by_name('password')
main.py:23: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  login_button = driver.find_element_by_xpath('//*[@id="app-mount"]/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]')
main.py:15: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  username_input = driver.find_element_by_name('email')
main.py:19: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  password_input = driver.find_element_by_name('password')
main.py:23: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  login_button = driver.find_element_by_xpath('//*[@id="app-mount"]/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]')
Traceback (most recent call last):
  File "main.py", line 19, in <module>
    driver.get("discord.com/login")
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 436, in get
main.py:15: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  username_input = driver.find_element_by_name('email')
main.py:19: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  password_input = driver.find_element_by_name('password')
main.py:23: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  login_button = driver.find_element_by_xpath('//*[@id="app-mount"]/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]')
Traceback (most recent call last):
  File "main.py", line 23, in <module>
    login_button = driver.find_element_by_xpath('//*[@id="app-mount"]/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 520, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
main.py:15: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  username_input = driver.find_element_by_name('email')
main.py:19: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  password_input = driver.find_element_by_name('password')
main.py:23: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  login_button = driver.find_element_by_xpath('//*[@id="app-mount"]/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]')
Traceback (most recent call last):
  File "main.py", line 23, in <module>
    login_button = driver.find_element_by_xpath('//*[@id="app-mount"]/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 520, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
main.py:15: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  username_input = driver.find_element_by_name('email')
main.py:19: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  password_input = driver.find_element_by_name('password')
main.py:23: DeprecationWarning: find_element_by_* commands are deprecated
Traceback (most recent call last):
  File "main.py", line 15, in <module>
    driver.add_cookie(cookie)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1128, in add_cookie
Traceback (most recent call last):
  File "main.py", line 15, in <module>
    driver.add_cookie(cookie)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1128, in add_cookie
Traceback (most recent call last):
  File "main.py", line 19, in <module>
    driver.get("discord.com/login")
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 436, in get
Traceback (most recent call last):
  File "main.py", line 19, in <module>
    driver.get("discord.com/login")
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 436, in get

任何帮助将不胜感激

【问题讨论】:

    标签: python selenium selenium-webdriver cookies


    【解决方案1】:

    在这一行:

    driver.get("discord.com/login")
    

    您将无效参数传递给 driver.get 方法,因为 discord.com/login 不是有效 URL。
    它应该是https://discord.com/login,因为您自己之前使用了几行。

    driver.get("https://discord.com/login")
    

    【讨论】:

    • 即使在此之后,如果是逻辑错误,cookie 注入问题也无法正常工作。我得到了同样的错误
    • 好的,现在的错误是什么?我根据您在问题中提出的错误回溯给出了答案。如果现在它仍然无法正常工作,那应该是另一个错误
    • 我最初根据新输出编辑了问题,请再看一遍。非常感谢你
    • 嗯,编辑问题是一种提出新问题,因为现在又是一个错误......
    • 您能否检查以下内容:1) 使用pickle.dump( driver.get_cookies() , open("cookies.pkl","wb")) 代码行验证cookie 是否正确保存。 2) cookies = pickle.load(open("cookies.pkl", "rb")) 给出了一个有效 cookie 的列表,以便 driver.add_cookie(cookie) 行得到一个有效的 cookie 对象?
    猜你喜欢
    • 1970-01-01
    • 2022-12-09
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多