简体   繁体   中英

How to invoke SendKeys() to the input element through Selenium and C#

Basically, I want to Send Keys to an input element. I tried using SendKeys() method but it doesn't send the value I used instead it uses the previous value attribute of the input box. Moreover, using SendKeys() or using ExecuteScript() methods doesn't change the value attribute of the input element. Below are the code pieces I have tried and didn't work so far :

// Wait for zipcode input box
Utilities.WaitUntilElementIsPresent(driver, By.CssSelector("input#fad-dealer-searchbar-search-textbox"));
// click to go to the us site
var zipcodeInput = driver.FindElement(By.CssSelector("input#fad-dealer-searchbar-search-textbox"));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

zipcodeInput.Clear();
zipcodeInput.Click();
//js.ExecuteScript("arguments[0].setAttribute('value', '11361')", zipcodeInput);
//js.ExecuteScript("document.getElementById('fad-dealer-searchbar-search-textbox').setAttribute('value', " + zipcode + ")");
//js.ExecuteScript("document.getElementById('fad-dealer-searchbar-search-textbox').value=11361");
//js.ExecuteScript("document.getElementById('fad-dealer-searchbar-search-textbox').innerHTML = 11361;");
zipcodeInput.SendKeys(zipcode);
zipcodeInput.Submit();
zipcodeInput.SendKeys(Keys.Enter);

Here is the input element that i need to enter the text to :

<div class="sdp-form-text-box-text-box-container search-textbox">      
  <input type="number" autocomplete="off" value="00002" data-validation-state="success" aria-invalid="false" aria-required="true" class="sdp-form-text-box-text-box gcss-button-align-left" id="fad-dealer-searchbar-search-textbox" maxlength="5" name="searchTextBoxValue" pattern="\d*" placeholder="Enter ZIP Code" required="" role="search">                                           
</div>

Here is the site I'm crawling if you guys need to check it Chrysler . I hope I have explained my issue properly. Any help is appreciated.

In the function "WaitUntilElementIsPresent" you need to check the expected conditions applied on webdriver. Following is the similar code. You can use "ElementIsVisible" or "ElementToBeClickable".

public static IWebElement WaitUntilElementIsPresent(this IWebDriver driver,By elementLocator, int timeout = 10)
    {
        try
        {
            var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout));
            return wait.Until(ExpectedConditions.ElementIsVisible(elementLocator));
        }
        catch (NoSuchElementException)
        {
            Console.WriteLine("Element with locator: '" + elementLocator + "' was not found.");
            throw;
        }
    }

To invoke SendKeys() you need to induce WebDriverWait in conjunction with ExpectedConditions as ElementToBeClickable and you can use either of the following solutions:

  • CssSelector :

     var zipcodeInput = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("label.gcss-sr-only[for^='change-zip__input-field-']"))); zipcodeInput.Click(); zipcodeInput.Clear(); zipcodeInput.SendKeys(zipcode); 
  • XPath :

     var zipcodeInput = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[@class='gcss-sr-only' and starts-with(@for, 'change-zip__input-field-')]"))); zipcodeInput.Click(); zipcodeInput.Clear(); zipcodeInput.SendKeys(zipcode); 

Note :

  • Ensure that zipcode consists of numbers only.
  • Ensure that zipcode maximum length is 5 digits only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM