簡體   English   中英

C#將Selenium與Chrome Portable和driver.Navigate()。GoToUrl結合使用

[英]C# Using Selenium with Chrome Portable and driver.Navigate().GoToUrl

我認為我要實現的目標相對簡單。 我正在嘗試編寫將使用Google Chrome瀏覽器便攜式可執行文件的代碼,並使用最新版的chrome驅動程序執行硒驅動的網頁元素查找和選擇。 目前,我知道如何做一個或另一個,但不能兩者都做。

以下代碼將從標准安裝位置(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");

        }
    }
}

但是,當我使用下面給出的自定義鑲邊位置二進制選項時,它將打開Goog​​le Chrome Portable,但不會轉到google.com。 相反,URL將僅說“ data:”,並且代碼將在Visual Studio中超時,並顯示以下消息:

“ OpenQA.Selenium.WebDriverException:'對URL http:// localhost:59127 / session的遠程WebDriver服務器的HTTP請求在60秒后超時。

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

我已經嘗試將鉻標志用於新窗口(-new-window + URL)和應用標志(--app + URL),但都無法運行應轉到google的代碼並在其中輸入“北極熊”搜索框。

協助將不勝感激。

非常感謝你。

賽斯

對於規格,我使用以下內容:

  • Windows 7的
  • Visual Studio社區版2017
  • Google Chrome Portable 68(還嘗試了舊版本的Chrome Portable)
  • Chrome驅動程序2.40(也嘗試使用2.41)

根據您的問題和代碼嘗試,我看不到任何重大缺陷。 也許按照其他討論 options.BinaryLocation必須指向便攜式chrome二進制文件絕對位置 ,如下所示:

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

您應該使用其他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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM