通过selenium firefox配置文件成功添加扩展.但是当我手动打开这些Firefox时,扩展仍然没有安装.<



我使用selenium python。我的代码工作成功,扩展被添加。但是当我关闭代码时,打开手动添加扩展的Firefox配置文件,然后扩展没有安装。我的代码

from selenium import webdriver从selenium.webdriver.common导入导入时间

试题:

path = "My_profile_PATH"
fp = webdriver.FirefoxProfile(path)     
driver = webdriver.Firefox(firefox_profile=fp)
# path to your downloaded Firefox addon extension XPI file
extension_path = "MY_extension_PATH"
# using webdriver's install_addon API to install the downloaded Firefox extension
driver.install_addon(extension_path, temporary=True)
# Opening the Firefox support page to verify that addon is installed
driver.get("about:support")
# xpath to the section on the support page that lists installed extension
addons = driver.find_element(By.XPATH,'//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]')
# scrolling to the section on the support page that lists installed extension
driver.execute_script("arguments[0].scrollIntoView();", addons)
# introducing program halt time to view things, ideally remove this when performing test automation in the cloud using LambdaTest
print("Success. Yayy!!")
time.sleep(20)

Exception as E:

print(E)

最后:

# exiting the fired Mozilla Firefox selenium webdriver instance
driver.quit()
# End Of Script

当您要求Selenium使用配置文件时,它会将其复制到一个临时位置并使用该副本。所以无论你做什么都不会影响原来的配置文件。

如果您希望脚本影响原始配置文件,请不要要求Selenium使用配置文件。相反,直接告诉Firefox使用哪个配置文件:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument('-profile')
options.add_argument('/path/to/your/profile')
driver = webdriver.Firefox(options=options)

如果有人对直接使用现有配置文件(不复制到新位置)并为下一个会话保留更改(缓存,cookie等)感兴趣,这里是我改进的Python Selenium 3.14.1版本,允许这种行为不像原始的Selenium。下面是这个版本的Github存储库:https://github.com/crspl/python-selenium-with-profiles