我已经用参数--start-debugger-server 61300
启动了firefox。
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.AddAdditionalCapability("debuggerAddress", "127.0.0.1:61300");
var d = new FirefoxDriver(firefoxOptions);
d.Navigate().GoToUrl("https://google.com/");
如何将驱动程序连接到firefox的运行实例,以便根据需要进行控制,而不是每次都用随机端口启动一个新实例?
经过测试,我找到了一个有效的方法。使用jsakamoto的Selenium.WebDriver.GeckoDriver.Win64 nuget包。
我使用命令行运行firefox:
"C:Program FilesMozilla Firefoxfirefox.exe" --marionette -foreground -no-remote
然后,我创建了默认服务,并使用端口2828进行连接,这是记录在案的提线木偶的默认端口。
FirefoxOptions firefoxOptions = new FirefoxOptions();
FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService();
firefoxDriverService.HideCommandPromptWindow = false;
firefoxDriverService.BrowserCommunicationPort = 2828;
firefoxDriverService.ConnectToRunningBrowser = true;
var driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
driver.Navigate().GoToUrl("https://youtube.com/");
如果已经有一个geckodriver实例在运行和连接,则需要首先关闭它。