11

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

        }


        }

    }

3 Answers 3

10

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

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:

Sign up to request clarification or add additional context in comments.

Comments

3

The clear command can be used to clear the element before entering any key in the text field

webDriver.FindElement(By.XPath("{Locator}")).Clear();
webDriver.FindElement(By.XPath("{Locator}")).SendKeys("{YOUR_KEY}");

surround this statement with try catch in order to catch and handle any selenium related exception

Comments

-1

Try This Before Sending Another Key:

IWebElement textbox= driver.FindElement(By.XPath(".//input[@type = 'text']"));
textbox.SendKeys(OpenQA.Selenium.Keys.LeftShift + OpenQA.Selenium.Keys.Home)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.