L.D*_*ter 3 c# selenium selenium-webdriver webdriverwait
我正在编写一个简单的 selenium 测试,它发送一个不正确的字母字符串,然后提交并返回和错误。然后我想发送一串字母,但这次使用正确的字符串,所以它被接受了。
我在测试中面临的问题是第一组字符串已经在文本字段中,所以当我去提交第二组时,它将它添加到已经提交的第一个字符串中。
所以我本质上需要做的是发送第一个字母字符串然后需要清除文本字段然后发送第二个字母字符串。
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();
String title = webDriver.Title;
String expectedTitle = "Utilities from Go Compare";
if (title.Contains(expectedTitle))
{
Console.WriteLine("Tile is matching with expected value");
}
else
{
Console.WriteLine("Tile is matching with expected value");
}
webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();
webDriver.FindElement(By.XPath(".//input[@type = 'text']")).SendKeys("W30PN#");
webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();
// I need a piece of code here so that before i send the second string of keys the text field is clear
webDriver.FindElement(By.XPath(".//input[@type = 'text']")).SendKeys("W30PN");
webDriver.FindElement(By.XPath(".//button[@type = 'submit']")).Click();
// webDriver.Quit();
}
}
}
Run Code Online (Sandbox Code Playgroud)
添加一行代码来清除该<input>字段,如下所示:
webDriver.FindElement(By.XPath(".//input[@type = 'text']")).Clear();
webDriver.FindElement(By.XPath(".//input[@type = 'text']")).SendKeys("W30PN");
Run Code Online (Sandbox Code Playgroud)
理想情况下,当您在调用 之后进入一个新页面时Navigate().GoToUrl(),您应该引入WebDriverWait以使<input> 元素可点击,您可以使用以下代码块:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath(".//input[@type='text']")));
element.Click();
element.Clear();
element.SendKeys("W30PN");
Run Code Online (Sandbox Code Playgroud)
您可以在以下位置找到一些相关讨论:
| 归档时间: |
|
| 查看次数: |
10184 次 |
| 最近记录: |