简体   繁体   English

如何处理网页硒C#上的错误

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

I've written code that goes onto https://energy.gocompare.com/gas-electricity . 我已经编写了https://energy.gocompare.com/gas-electricity上的代码。

I want to introduce another parameter in my code. 我想在代码中引入另一个参数。 So when i go onto the webpage and click the continue button without entering any data i get an validation error on the page 'Please provide your postcode, as different regions have different fuel prices.' 因此,当我进入网页并在不输入任何数据的情况下单击继续按钮时,页面上出现验证错误,“请提供您的邮政编码,因为不同地区的燃油价格不同。”

How can i introduce a check in my code so that it validates this message displayed is correct. 我如何在我的代码中引入检查,以便验证所显示的消息是否正确。 I've tried to find an XPath but i'm not sure that is the right way to go about it 我试图找到一个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();

i tried this cssSelector 我尝试了这个cssSelector

     div[class$='validation-error']

Looking fetching error message. 寻找错误信息。

In Java+TestNG, usually will do assertions 在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.");

you need to wait for validation error element: 您需要等待验证错误元素:

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());
            }
    }           
}   

After clicking on continue button, you can try this code : 点击继续按钮后,您可以尝试以下代码:

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