簡體   English   中英

在 C# 中使用 Selenium Chrome Webdriver 時出錯

[英]Error using Selenium Chrome Webdriver with C#

我使用 Selenium Chrome Webdriver 制作了一個程序。 我從一個已知網站上獲取了文本。 當它工作並測試幾天時,它完美無缺。 但是有一天使用相同的代碼運行它時出現此錯誤“System.InvalidOperationException:'會話未創建:此版本的ChromeDriver僅支持Chrome版本83(SessionNotCreated)'”

它說錯誤在這行代碼上: driver = new ChromeDriver(options);

我有來自 Selenium 的最新更新,但 Visual Studio 版本是 Chrome WebDriver 的 83.0.0,而我的 Chrome 版本是 85.0.4183.83。 我該怎么辦?

這是我的代碼:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Security.Cryptography;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Chrome;

public static class Program
    {


        public static void Main()
        {

            IWebDriver driver;
            
            var options = new ChromeOptions();
            options.AddArgument("headless");
            options.AddArgument("--disable-gpu");
            
            
            driver = new ChromeDriver(options);
            driver.Navigate().GoToUrl("");
            string currentDay = DateTime.Today.DayOfWeek.ToString();
            System.Threading.Thread.Sleep(5000);
            switch(currentDay)
            {
            case "Monday":
                var MondayFood = driver.FindElement(By.XPath("/html/body/main/article/section[1]/div/div[1]/div[2]/div[2]/div/div/div[1]/ul/li[1]/p[1]/span[2]"));
                System.Console.WriteLine(MondayFood.Text);

                break;

            case "Tuesday":
                var TuesdayFood = driver.FindElement(By.XPath("/html/body/main/article/section[1]/div/div[1]/div[2]/div[2]/div/div/div[2]/ul/li[1]/p[1]/span[2]"));
                System.Console.WriteLine(TuesdayFood.Text);
                break;

            case "Wednesday":
                var WednesdayFood = driver.FindElement(By.XPath("/html/body/main/article/section[1]/div/div[1]/div[2]/div[2]/div/div/div[3]/ul/li[1]/p[1]/span[2]"));
                System.Console.WriteLine(WednesdayFood.Text);
                break;

            case "Thursday":
                var ThursdayFood = driver.FindElement(By.XPath("/html/body/main/article/section[1]/div/div[1]/div[2]/div[2]/div/div/div[4]/ul/li[1]/p[1]/span[2]"));
                System.Console.WriteLine(ThursdayFood.Text);
                break;

            case "Friday":
                var FridayFood = driver.FindElement(By.XPath("/html/body/main/article/section[1]/div/div[1]/div[2]/div[2]/div/div/div[5]/ul/li[1]/p[1]/span[2]"));
                System.Console.WriteLine(FridayFood.Text);
                break;
                
            }

        System.Console.ReadLine();
             
        
        
    }

        

    }

您的 chrome 網絡驅動程序版本需要與您 PC 上安裝的 google chrome 版本匹配。

請使用以下鏈接下載 chromedriver。

鉻驅動程序

首先,按照以下說明檢查您的 Chrome 版本:

  1. 單擊窗口右上角的三個點。
  2. 將光標懸停在“幫助”上。
  3. 點擊“關於谷歌瀏覽器”。 您的 Google Chrome 瀏覽器的版本將出現在屏幕頂部的下一個窗口中。

然后下載匹配的 chrome webdriver 版本。 通過此鏈接: https : //chromedriver.chromium.org/downloads

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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