使用硒(包括firefox附加组件)启动firefox

时间:2019-05-26 02:42:55

标签: python selenium firefox options

我正在使用Python 3.x和Selenium,均已更新。尝试使用完好无损的Selenium启动Firefox。即使是我尝试过的关于Stackoverflow的相关答案,也没有任何效果。那里有Selenium向导吗?

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
options = Options
fp = webdriver.FirefoxProfile('<path to my one firefox profile which     includes add-ons at launch')
driver = webdriver.Firefox(executable_path='<path to geckodriver>', firefox_profile=fp)

我希望Selenium可以在不影响我的附加组件的情况下启动Firefox。具体来说,我希望随机用户代理切换器能够正常运行,即定期更改用户代理。

1 个答案:

答案 0 :(得分:1)

它会稍微改变您的布局。但是在与Selenium纠缠了一段时间之后,WebDriver的以下实现确实帮助我设置了optionsprofile之类的属性。

from selenium import webdriver

# tag options field
options = webdriver.FirefoxOptions()  
# disable push/popups 
options.set_preference("dom.push.enabled", False)  

# tag profile option
profile = webdriver.FirefoxProfile() 
# what's the profile then (normally imported from separate infos.py)
user_agent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) \
              AppleWebKit/528.18 (KHTML,like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"
# adjust profile to reflect user_agent profile
profile.set_preference("general.useragent.override", user_agent)

# set driver with options 
driver = webdriver.Firefox(options=options, firefox_profile=profile)

很高兴阅读您能够找到答案。希望这会有所帮助或/并且将来可以使用。祝你有美好的一天!

相关问题