简体   繁体   English

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

[英]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))打开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");

        }
    }
}

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. 但是,当我使用下面给出的自定义镶边位置二进制选项时,它将打开Goog​​le 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: 对于规格,我使用以下内容:

  • Windows 7 Windows 7的
  • Visual Studio Community Edition 2017 Visual Studio社区版2017
  • Google Chrome Portable 68 (Also tried older versions of Chrome portable) Google Chrome Portable 68(还尝试了旧版本的Chrome Portable)
  • Chrome Driver 2.40 (also tried 2.41) Chrome驱动程序2.40(也尝试使用2.41)

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.

相关问题 Selenium driver.Url 与 driver.Navigate().GoToUrl() - Selenium driver.Url vs. driver.Navigate().GoToUrl() 使用Firefox便携式的C#硒webdriver - c# selenium webdriver using firefox portable C#硒中的Chrome驱动程序问题 - Chrome Driver issues in C# selenium C# Selenium Chrome 驱动程序未等待 - C# Selenium Chrome Driver isnt waiting 使用C#将驱动程序导航到硒中新打开的窗口 - Navigate driver to new opened window in selenium with C# 如何使用c#和Selenium chrome驱动程序加载特定的Google Chrome配置文件? - How to load specific google chrome profile using c# & Selenium chrome driver? 如何使用 C# 中的 Selenium chrome 驱动程序从网页打印 URL 列表以进行控制台? - How do I print a list of URLs to console from a webpage using Selenium chrome driver in C#? 使用Gallio和Powershell和Chrome Driver在MbUnit框架和Selenium中进行C#并行测试失败-所有测试均失败 - Failed Parallel tests in C# with MbUnit framework and Selenium using Gallio and powershell with the Chrome Driver - all tests fail Selenium IWebDriver Navigate()。GoToUrl()没有输入url或导航到页面 - Selenium IWebDriver Navigate().GoToUrl() not entering url or navigating to page Chrome 未使用 GoToUrl 方法打开 url - Chrome is not opening the url by using GoToUrl Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM