【问题标题】:C# Using Selenium with Chrome Portable and driver.Navigate().GoToUrlC# 将 Selenium 与 Chrome 便携版和 driver.Navigate().GoToUrl 一起使用
【发布时间】: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


    【解决方案1】:

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

    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);
    

    【讨论】:

    • 尝试了很多东西后唯一对我有用的答案。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      根据您的问题和代码尝试,我没有看到任何重大缺陷。也许根据other discussions options.BinaryLocation 必须指向 portable chrome binary绝对位置,如下所示:

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

      【讨论】:

      • 感谢@DebanjanB 的努力。对此,我真的非常感激。但是,使用绝对位置并将默认的“GoogleChromePortable.exe”重命名为“chrome.exe”会产生相同的结果。 :( 网页 URL 仍然只是简单地说“数据:”。
      • 我也试过你链接的python代码,它也无法加载网站。谷歌浏览器只是在 URL 中显示“data:”,并打开一个空白的 CMD 窗口。还有其他建议吗?
      猜你喜欢
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      相关资源
      最近更新 更多