当前位置: 首页 > 知识库问答 >
问题:

C#使用Selenium和Chrome便携和driver.navigate().gotourl

曹泉
2023-03-14

我想要完成的是相对简单的。我试图编写代码,将使用谷歌Chrome可移植的可执行文件,也执行selenium powered网页元素查找和选择使用最新版本的Chrome驱动程序。目前,我知道如何做其中一个或另一个,但不是两者都做。

下面的代码将从Google Chrome的标准安装位置(C:Program Files(x86)打开它,并在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");

        }
    }
}
options.BinaryLocation = @"C:\GoogleChromePortable\GoogleChromePortable.exe";
  • Windows 7
  • Visual Studio社区版2017
  • Google Chrome Portable 68(也尝试了Chrome Portable的旧版本)
  • Chrome Driver 2.40(也尝试过2.41)

共有1个答案

严劲
2023-03-14

您应该使用另一个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);
 类似资料:
  • 本文向大家介绍sed便携使用,包括了sed便携使用的使用技巧和注意事项,需要的朋友参考一下 示例 就地编辑虽然很常见,但却是一种非标准功能。一个可行的替代方法是使用中间文件来存储原始文件或输出。 要将-i选项与GNU和FreeBSD语法一起使用,必须指定扩展名并将其附加到-i选项中。两者都将接受以下内容,并产生两个文件:的原始版本file.orig和的编辑版本file: 请参阅给定文件的基本示例f

  • 我的Selenium web驱动程序有问题。我正在尝试做的是启动一个“便携”chrome而不是我的本地安装,因为它有不同的设置。 问题是,便携式Chrome(来自PortableApps)似乎只有在使用GoogleChromePortable.exe时才会启动。如果我直接使用Chrome二进制,它将启动我的本地安装。对于Selenium,无论我传递给它的是什么Chrome路径(GoogleChro

  • 问题内容: 我的脚本必须使用一组凭据登录到站点,然后在某些下拉菜单中导航以选择报告。选择报告后,将弹出一个新窗口,必须在其中调整参数以生成报告。设置完参数后,相同的弹出窗口将以生成的PDF格式刷新报告,并使用Chrome的内置PDF查看器显示。我的印象是,将某些选项传递给webdriver会禁用此PDF查看器,而只是下载文件,但是PDF查看器仍在显示,不会自动下载任何内容。我当然会丢失某些东西,或

  • 我试着跟随以前关于这个主题的帖子,比如这些(帖子1,帖子2),但我仍然被困住了。 所以,在这一点上,Chrome仍然显示PDF查看器,尽管我之前禁用了它。没有下载任何东西,所以我想知道我是否需要提供另一行代码或其他东西。 在Windows 10上使用Selenium版本3.141.0、Python 3.6.4、Chrome webdriver 2.45。

  • 我找不到任何解决这个问题的办法,这就是为什么张贴在这里。对不起,这个愚蠢的问题。 我无法使用selenium网络驱动程序启动任何浏览器。请在下面找到详细信息: 我的电脑中的Chrome版本:55.0.2883.87 m版 Chrome路径:C:\Program Files\Google\Chrome\Application\chrome.exe Chrome驱动程序版本:2.26 使用seleni

  • 我尝试了这个python脚本在windows上的portable Firefox中打开一个url,但是我得到了跟踪。 以下是回溯: 我正在使用: 你知道如何让便携式Firefox工作吗?