c# - 如何处理网页 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();
解决方案
单击继续按钮后,您可以尝试以下代码:
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());
推荐阅读
- image-processing - gimp bicubic和standard bicubic的区别问题
- java - Log4j 不创建日志文件
- xml - 读取 xml 文件 .findall() “轨迹”
- javascript - 如何根据用户信息显示不同的侧边栏?登录后。ReactJS
- java - 无法在我的画廊中保存位图图像
- javascript - ionic 2 中的手风琴显示不规则显示
- spring - serviceId 的 Zuul 路由不起作用
- c++ - 为什么我收到此错误“从'A'类型的右值对'A&'类型的非常量引用的无效初始化”
- git - 与 git checkout 合并
- javascript - Redux Saga 观察者差异