Skip to main content
 首页 » 编程设计

python之使用 selenium python 和 Firefox 重新打开相同的浏览器窗口

2024年09月07日8Free-Thinker

嘿,我正在尝试制作一个自动程序来发送 Whatsapp 消息。 我目前正在使用 python、Firefox 和 selenium 来实现这一目标。 问题是每次我调用 driver.get(url) 它都会打开一个新的 firefox 浏览器实例,空白,没有上次运行的内存。它让我每次运行时都扫描条形码。

from selenium import webdriver 
from selenium.webdriver.firefox.webdriver import FirefoxProfile 
 
cp_profile = webdriver.FirefoxProfile("/Users/Hodai/AppData/Roaming/Mozilla/Firefox/Profiles/v27qat5d.whatsapp_profile") 
driver = webdriver.Firefox(executable_path="/Users/Hodai/Desktop/geckodriver",firefox_profile=cp_profile) 
driver.get('http://web.whatsapp.com') 
 
#Scan the code before proceeding further 
input('Enter anything after scanning QR code') 

我试过使用配置文件,但似乎没有任何影响。

cp_profile = webdriver.FirefoxProfile("/Users/Hodai/AppData/Roaming/Mozilla/Firefox/Profiles/v27qat5d.whatsapp_profile") 
driver = webdriver.Firefox(executable_path="/Users/Hodai/Desktop/geckodriver",firefox_profile=cp_profile) 

请您参考如下方法:

最后我使用 chromedriver 来实现我的目标。 我尝试用 pickle 做 cookies,但这有点棘手,因为它只记住同一个域的 cookies。 所以我使用了 chrome 的用户数据。 现在它就像一个魅力。谢谢大家。

from selenium import webdriver 
from selenium.webdriver.chrome.options import Options 
 
options = Options() 
options.add_argument("user-data-dir=C:/Users/Designer1/AppData/Local/Google/Chrome/User Data/Profile 1") 
driver = webdriver.Chrome(chrome_options=options,executable_path="C:\webdrivers\chromedriver.exe")