【发布时间】:2018-08-06 22:09:22
【问题描述】:
我想,我想要完成的是相对简单的。我正在尝试编写将使用 Google Chrome Portable 可执行文件的代码,并使用最新版本的 chrome 驱动程序执行 selenium 驱动的网页元素查找和选择。目前,我知道如何做其中之一,但不知道两者都做。
以下代码将从其标准安装位置(C:Program Files (x86))打开 Google Chrome,并在 Google 搜索框中输入“北极熊”的文本。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace LaunchChrome
{
class GoogleInquiry
{
static void Main(string[] args)
{
//Start Chrome Driver Service from Custom Location
ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"C:\GoogleSearch");
//Force the CMD prompt window to automatically close and suppress diagnostic information
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
//Setup Chrome to utilize custom options
var options = new ChromeOptions();
//Google Chrome Custom Options
options.AddArgument("disable-infobars");
options.AddArgument("--silent");
// Assign driver variable to Chrome Driver
var driver = new ChromeDriver(service, options);
//Navigate to the Google website
driver.Navigate().GoToUrl("https://www.google.com");
//Automate custom Google Search Submission
driver.FindElement(By.Name("q")).SendKeys("Polar Bears");
}
}
}
但是,当我使用下面给出的自定义 chrome 位置二进制选项时,它会打开 Google Chrome Portable,但不会转到 google.com。相反,该 URL 将简单地说“数据:”,并且代码将在 Visual Studio 中超时并显示以下消息:
"OpenQA.Selenium.WebDriverException: '对 URL http://localhost:59127/session 的远程 WebDriver 服务器的 HTTP 请求在 60 秒后超时。'
options.BinaryLocation = @"C:\GoogleChromePortable\GoogleChromePortable.exe";
我已经尝试对新窗口使用 chromium 标志(--new-window + URL)并使用应用标志(--app +URL),但两者都无法运行应该转到谷歌并输入“Polar”的代码熊”在搜索框中。
我们将不胜感激。
非常感谢。
赛斯
对于规格,我使用以下内容:
- Windows 7
- Visual Studio 社区版 2017
- 谷歌浏览器 Portable 68(也尝试过旧版本的 Chrome 便携版)
- 铬 驱动程序 2.40(也尝试过 2.41)
【问题讨论】:
标签: c# google-chrome selenium selenium-webdriver selenium-chromedriver