簡體   English   中英

如何處理網頁硒C#上的錯誤

[英]How to handle an error on a webpage selenium c#

我已經編寫了https://energy.gocompare.com/gas-electricity上的代碼。

我想在代碼中引入另一個參數。 因此,當我進入網頁並在不輸入任何數據的情況下單擊繼續按鈕時,頁面上出現驗證錯誤,“請提供您的郵政編碼,因為不同地區的燃油價格不同。”

我如何在我的代碼中引入檢查,以便驗證所顯示的消息是否正確。 我試圖找到一個XPath,但是我不確定這是正確的方法

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Exercise1
{
    class RunPath
    {

            static void Main()
            {

                IWebDriver webDriver = new ChromeDriver();
                webDriver.Navigate().GoToUrl("https://energy.gocompare.com/gas-electricity");
                webDriver.Manage().Window.Maximize();

webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();

我嘗試了這個cssSelector

     div[class$='validation-error']

尋找錯誤信息。

在Java + TestNG中,通常會做斷言

  Assert.assertEquals(driver.findElement(By.cssSelector("div[class$='validation-error']")).getText().trim(), "Please provide your postcode, as different regions have different fuel prices.");

您需要等待驗證錯誤元素:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Exercise1
{
    class RunPath
    {
            static void Main()
            {
                IWebDriver webDriver = new ChromeDriver();
                webDriver.Navigate().GoToUrl("https://energy.gocompare.com/gas-electricity");
                webDriver.Manage().Window.Maximize();

                webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();

                WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
                IWebElement error = wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[contains(@class, 'validation-error')]")));
                Assert.AreEqual("Please provide your postcode, as different regions have different fuel prices.", validationError.Text.TextTrim());
            }
    }           
}   

點擊繼續按鈕后,您可以嘗試以下代碼:

WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
IWebElement error = wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[contains(@class, 'validation-error')]")));
Assert.AreEqual("Please provide your postcode, as different regions have different fuel prices.", error.Text.TextTrim());

暫無
暫無

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

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