我正在尝试让Python Selenium在我的Windows机器上运行。我已升级到最新版本的Firefox,Selenium,Geckodriver,但我仍然收到以下错误:
Python脚本
from selenium import webdriver
driver = webdriver.Firefox()
错误
Traceback (most recent call last):
File "run.py", line 17605, in <module>
File "<string>", line 21, in <module>
File "site-packages\selenium\webdriver\firefox\webdriver.py", line 77, in __init__
File "site-packages\selenium\webdriver\firefox\extension_connection.py", line 49, in __init__
File "site-packages\selenium\webdriver\firefox\firefox_binary.py", line 68, in launch_browser
File "site-packages\selenium\webdriver\firefox\firefox_binary.py", line 103, in _wait_until_connectable
WebDriverException: Message: Can't load the profile. Profile Dir: %s If you specified a log_file in the FirefoxBinary constructor, check it for details.
我也尝试使用以下代码创建firefox配置文件:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
profile.set_preference('general.warnOnAboutConfig', False)
gecko_path = "path_to_geckodriver\\geckodriver.exe"
path = "path_to_firefoxs\\Mozilla Firefox\\firefox.exe"
binary = FirefoxBinary(path)
driver = webdriver.Firefox(firefox_profile=profile,executable_path=gecko_path)
答案 0 :(得分:2)
您没有更新配置文件的首选项: profile.update_preferences()。 设置首选项后,您必须更新配置文件。请遵循以下代码:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
profile.set_preference('general.warnOnAboutConfig', False)
profile.update_preferences()
gecko_path = "path_to_geckodriver\\geckodriver.exe"
path = "path_to_firefoxs\\Mozilla Firefox\\firefox.exe"
binary = FirefoxBinary(path)
driver = webdriver.Firefox(firefox_profile=profile,executable_path=gecko_path)
答案 1 :(得分:1)
我一直在努力,直到找到this issue为止,该问题现已解决,但由于显示了一些命令而很有用。
from selenium import webdriver
fp = webdriver.FirefoxProfile('/home/gabriel/.mozilla/firefox/whatever.selenium')
driver = webdriver.Firefox(fp)
第一个版本,由于以后无法连接硒而无法正常工作:
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
options = Options()
options.add_argument("-profile")
options.add_argument("/home/gabriel/.mozilla/firefox/whatever.selenium")
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_options=options)
我确信这会加载配置文件“ whatever.selenium”,因为如果我转到about:profiles,我会读到:
配置文件:硒 这是正在使用的配置文件,无法删除。
即使“ whatever.selenium”不是系统上的默认配置文件。
备注:硒(或geckodriver?)至少覆盖了一个配置文件参数:首选项>隐私和安全性>“阻止弹出窗口”始终重置为关闭状态。因此,使用about:profiles可以断言您正在运行的配置文件。
注释:
firefox_capabilities
。答案 2 :(得分:0)
切换到Chrome驱动程序。 Firefox,gecko和selenium现在不能很好地协同工作。这是最终为我工作的。
import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
class TestTemplate(unittest.TestCase):
"""Include test cases on a given url"""
def setUp(self):
"""Start web driver"""
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.driver.implicitly_wait(10)
def tearDown(self):
"""Stop web driver"""
self.driver.quit()
def test_case_1(self):
"""Go to python.org and print title"""
try:
self.driver.get('https://www.python.org/')
title = self.driver.title
print title
except NoSuchElementException as ex:
self.fail(ex.msg)
def test_case_2(self):
"""Go to redbull.com and print title"""
try:
self.driver.get('https://www.redbull.com')
title = self.driver.title
print title
except NoSuchElementException as ex:
self.fail(ex.msg)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestTemplate)
unittest.TextTestRunner(verbosity=2).run(suite)
我使用Jenkins文件加载帧缓冲区来调用selenium和python脚本。
您可以在本地计算机上轻松运行此功能。你可能想让一个流浪汉的盒子用linux。
这是启动python脚本的原因。
sh&#34;(Xvfb:99-screen 0 1366x768x16&amp;)&amp;&amp; (python ./$ {PYTHON_SCRIPT_FILE})&#34;
从运行此Docker镜像的docker文件调用此方法。
答案 3 :(得分:0)
在java中加载自定义的firefox配置文件:
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
答案 4 :(得分:0)
在Windows上,我使用:
height: 100vh
1-要查找当前的fp = webdriver.FirefoxProfile('C:/Users/x/AppData/Roaming/Mozilla/Firefox/Profiles/some-long-string')
driver = webdriver.Firefox(firefox_profile=fp)
...
,请在url字段上键入Profile Folder
,然后按Enter。
2-要查看所有用户个人资料,请在url字段上键入about:support
,然后按Enter。