如何在发送键硒C#前清除文本字段 [英] How to clear text field before sending keys selenium c#
问题描述
我正在编写一个简单的硒测试,该测试将发送不正确的字母字符串,然后提交并返回错误.然后,我想发送一个字母字符串,但是这次使用正确的字符串,这样它就可以被接受了.
I'm writing a simple selenium test which sends an incorrect string of letters then submits and returns and error. I want to then send a string of letter but this time with the correct string so it is accepted.
我在测试中面临的问题是,第一组字符串已经在文本字段中,因此当我提交第二个字符串时,它将其添加到已经提交的第一个字符串上.
The issue i'm facing in my test is as the first set of string is already in the text field, so when i go to the submit the second one it adds it on to the first string that has already been submitted.
所以我本质上需要做的是发送第一个字母字符串,然后需要清除文本字段,然后发送第二个字母字符串.
So what i essentially need to do is send the first string of letters then need to clear the text field then send the second string of letters.
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();
}
}
}
推荐答案
添加一行代码以清除<input>
字段,如下所示:
Add a line of code to clear the <input>
field as follows:
webDriver.FindElement(By.XPath(".//input[@type = 'text']")).Clear();
webDriver.FindElement(By.XPath(".//input[@type = 'text']")).SendKeys("W30PN");
理想情况下,就像您在调用Navigate().GoToUrl()
后进入新页面一样,您应该诱使 WebDriverWait 成为<input>
元素可点击,并且您可以使用以下代码块:
Ideally, as you are on a new page after invoking Navigate().GoToUrl()
, you should induce WebDriverWait for the <input>
element to be clickable and you can use the following code block:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath(".//input[@type='text']")));
element.Click();
element.Clear();
element.SendKeys("W30PN");
参考
您可以在以下位置找到一些相关的讨论:
Reference
You can find a couple of relevant discussions in:
- clear() does not clear the textbox with selenium and python and firefox
- InvalidElementStateException when attempting to clear text through Selenium
- python selenium can't clear input field
这篇关于如何在发送键硒C#前清除文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!