C# 在 Chrome Portable 和 driver.Navigate().GoToUrl 中使用 Selenium

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

我想我想要完成的是相对简单的。我正在尝试编写将使用 Google Chrome Portable 可执行文件的代码,并使用最新版本的 chrome 驱动程序执行 selenium 驱动的网页元素查找和选择。目前,我知道如何做其中之一,但不知道两者。

以下代码将从标准安装位置(C:Program Files (x86))打开 Google Chrome,并在 Google 搜索框中输入“Polar Bears”文本。

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

我已经尝试使用新窗口的 Chrome 标志(--new-window + URL)和使用应用程序标志(--app + URL)但是都无法运行应该去谷歌并输入“Polar熊”在搜索框中。

我们将不胜感激。

非常感谢。

赛斯

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

  • Windows 7
  • Visual Studio 社区版 2017
  • 谷歌浏览器 可移植 68(也尝试过旧版本的 Chrome 可移植)
  • Chrome 驱动程序 2.40(也试过 2.41)

最佳答案

您应该使用另一个 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);

关于C# 在 Chrome Portable 和 driver.Navigate().GoToUrl 中使用 Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716291/

相关文章:

c# - Microsoft.Extensions.Logging 记录器包装器的实现和使用

ios - 如何防止移动 Chrome/Safari 中的对 Angular 滚动

c# - 我有开始时间、结束时间和持续时间..我想在开始时间中添加持续时间,直到在 C# 中实现结束时间?

c# - 当使用 System.Net.Mail 发送时,HTML4.0 SSRS 报告在 Outlook 中被压缩。它可以修复吗?

javascript - 响应式部署的网站显示可以在 Firefox 的 chrome devtools 中工作,但不能在实际移动设备上的 chrome 中工作?

html - CSS 不透明度属性在 Chrome 上不起作用

c# - Chrome驱动Selenium下载位置

javascript - Selenium Python : Cannot find element after javascript runs

javascript - 通过 Selenium 抓取动态内容?

c# - 如何在 xamarin.android 中使用 HttpClient 将数据传递到服务器