[英]C# Using Selenium with Chrome Portable and driver.Navigate().GoToUrl
What I am trying to accomplish is, I think, relatively simple. 我认为我要实现的目标相对简单。 I am trying to write code that will use a Google Chrome Portable executable and also execute selenium powered web-page element finding and selecting using the latest version of the chrome driver.
我正在尝试编写将使用Google Chrome浏览器便携式可执行文件的代码,并使用最新版的chrome驱动程序执行硒驱动的网页元素查找和选择。 Currently, I know how to do one or the other, but not both.
目前,我知道如何做一个或另一个,但不能两者都做。
The following code will open Google Chrome from it's standard installation location (C:Program Files (x86) and input the text for "Polar Bears" in the Google Search box. 以下代码将从标准安装位置(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");
}
}
}
But, when I use the custom chrome location binary option given below, than it will open Google Chrome Portable, but it won't go to google.com. 但是,当我使用下面给出的自定义镶边位置二进制选项时,它将打开Google Chrome Portable,但不会转到google.com。 Rather, the URL will say simply "data:," and the code will timeout in visual studio with this message:
相反,URL将仅说“ data:”,并且代码将在Visual Studio中超时,并显示以下消息:
"OpenQA.Selenium.WebDriverException: 'The HTTP request to the remote WebDriver server for URL http://localhost:59127/session timed out after 60 seconds.' “ OpenQA.Selenium.WebDriverException:'对URL http:// localhost:59127 / session的远程WebDriver服务器的HTTP请求在60秒后超时。
options.BinaryLocation = @"C:\GoogleChromePortable\GoogleChromePortable.exe";
I have tried using the chromium flag for new window (-- new-window + URL) and using the app flag (--app +URL) but both fail to run the code that should go to google and input "Polar Bears" in the search box. 我已经尝试将铬标志用于新窗口(-new-window + URL)和应用标志(--app + URL),但都无法运行应转到google的代码并在其中输入“北极熊”搜索框。
Assistance would be appreciated. 协助将不胜感激。
Thank you very much. 非常感谢你。
Seth 赛斯
For Specs, I am using the following: 对于规格,我使用以下内容:
As per your question and code attempts I don't see any any major flaw. 根据您的问题和代码尝试,我看不到任何重大缺陷。 Perhaps as per other discussions
options.BinaryLocation
must point to the absolute location of the portable chrome binary as follows: 也许按照其他讨论
options.BinaryLocation
必须指向便携式chrome二进制文件的绝对位置 ,如下所示:
options.BinaryLocation = @"C:\\GoogleChromePortable\\chrome.exe";
You should use the other chrome.exe location the one which is inside Chrome-bin folder 您应该使用其他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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.