如何处理网页硒C#上的错误 [英] How to handle an error on a webpage selenium c#

查看:92
本文介绍了如何处理网页硒C#上的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了> 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.'

如何在代码中引入检查,以验证显示的消息是否正确.我试图找到一个XPath,但是我不确定这是否是正确的方法

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

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

推荐答案

单击继续按钮后,您可以尝试以下代码:

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

这篇关于如何处理网页硒C#上的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆