我想在页面加载之前运行一个JS脚本,所以我把它放在了TAMPER猴子中。但在驱动程序关闭后,脚本不会持续存在。如果我再次运行代码,保存的脚本将不复存在。这是在python中运行selenium的代码。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
opts = Options()
opts.add_extension("tampermonkey extension.zip")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=opts)
driver.get("url")
# Add the JS script in Tampermonkey in the browser manually
driver.close()
发布于 2019-01-23 22:48:59
要在驱动程序“回收”之后持久化任何扩展或特定设置,您唯一的选择就是拥有一个自定义配置文件,并告诉selenium使用该配置文件。
在Chrome中有一个很好的答案:How to load default profile in chrome using Python Selenium Webdriver?
在火狐中,Selenium:https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver直接支持,请参见webdriver.Firefox()
构造函数https://selenium-python.readthedocs.io/api.html#selenium.webdriver.firefox.firefox_profile.FirefoxProfile的第一个参数
https://stackoverflow.com/questions/54329519
复制相似问题