C#将Selenium与Chrome Portable和driver.Navigate()。GoToUrl结合使用

时间:2018-08-06 22:09:23

标签: c# google-chrome selenium selenium-webdriver selenium-chromedriver

我认为我要完成的工作相对简单。我正在尝试编写将使用Google Chrome浏览器便携式可执行文件的代码,并使用最新版的chrome驱动程序执行硒驱动的网页元素查找和选择。目前,我知道该怎么做,但不能两者都做。

以下代码将从标准安装位置(C:Program Files(x86))打开Goog​​le 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");

        }
    }
}

但是,当我使用下面提供的自定义镶边位置二进制选项时,它将打开Goog​​le Chrome Portable,但不会转到google.com。相反,URL将仅说“ data:”,并且代码将在Visual Studio中显示以下消息:

“ OpenQA.Selenium.WebDriverException:'对URL http://localhost:59127/session的远程WebDriver服务器的HTTP请求在60秒后超时。'

options.BinaryLocation = @"C:\GoogleChromePortable\GoogleChromePortable.exe";

我尝试将Chrome标志用于新窗口(-new-window + URL)和app标志(--app + URL),但都无法运行应转到google并输入“ Polar熊”。

不胜感激。

非常感谢您。

塞思

对于规格,我使用以下内容:

  • Windows 7
  • Visual Studio Community Edition 2017
  • 谷歌浏览器 便携式68(还尝试了旧版的Chrome便携式)
  • Chrome 驱动程序2.40(也尝试使用2.41)

3 个答案:

答案 0 :(得分:1)

我知道这是一个老问题,但是我找到的答案是,如果我将Google Chrome企业版安装文件复制到计算机上的其他位置,然后将代码指向chrome.exe二进制绝对值位置,比它有用。

答案 1 :(得分:0)

根据您的问题和代码尝试,我看不到任何重大缺陷。也许按照other discussions options.BinaryLocation 必须指向便携式Chrome二进制文件绝对位置,如下所示:

options.BinaryLocation = @"C:\\GoogleChromePortable\\chrome.exe";

答案 2 :(得分:0)

您应该使用另一个chrome.exe位置,即Chrome-bin文件夹中的一个位置

String seStandAloneServerUrl = @"the stand alone server url here";
var cOptions = new ChromeOptions();
cOptions.BinaryLocation = @"C:\GoogleChromePortable\App\Chrome-bin\chrome.exe";
driver = new RemoteWebDriver(new Uri(seStandAloneServerUrl), cOptions);