gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:15:31 28 4
gpt4 key购买 nike

我想我想要完成的是相对简单的。我正在尝试编写将使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com