我想在使用 Selenium WebDriver (geckodriver) 时保持 Firefox 登录状态。我使用了下面的python代码:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
try:
fp = webdriver.FirefoxProfile('C:/Users/admin/AppData/Roaming/Mozilla/Firefox/Profiles/mow8ff69.selenium')
options = Options()
options.headless = False
browser = webdriver.Firefox(fp, options=options)
browser.get('https://stackoverflow.com/users/login');
time.sleep(2)
txt_username = browser.find_element_by_id('email')
txt_username.clear()
txt_username.send_keys('***@gmail.com') #replace with your own username
time.sleep(1)
txt_password = browser.find_element_by_id('password')
txt_password.clear()
txt_password.send_keys('*****') #replace with your own password
time.sleep(1)
txt_password.submit()
time.sleep(10)
except Exception as ex:
print('Error: {0}'.format(ex))
finally:
browser.quit()
如上所示,我一直在使用由“Firefox -p”创建的用户配置文件。但是,在运行上述代码后,当我使用该配置文件重新打开 Firefox 并转到 stackoverflow.com 时,我仍然没有登录。
所以看起来Selinum WebDriver实际上在一个根本无法写入用户配置文件的“沙箱”中运行firefox?我知道 ChromeDriver 有一个“--no-sandbox”选项,但它似乎在 geckodriver 中不可用?有什么解决办法吗?
我无法切换到 ChromeDriver,因为它还有另一个错误:“--headless”选项实际上与“--user-data-dir”选项冲突,而我确实需要两者。