简体   繁体   English

如何通过Selenium和C#对输入元素调用SendKeys()

[英]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. 我尝试使用SendKeys()方法,但它不发送使用的值,而是使用输入框的上一个value属性。 Moreover, using SendKeys() or using ExecuteScript() methods doesn't change the value attribute of the input element. 而且,使用SendKeys()ExecuteScript()方法不会更改输入元素的value属性。 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. 在功能“ WaitUntilElementIsPresent”中,您需要检查应用于webdriver的预期条件。 Following is the similar code. 以下是类似的代码。 You can use "ElementIsVisible" or "ElementToBeClickable". 您可以使用“ ElementIsVisible”或“ 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: 要调用SendKeys()您需要将WebDriverWaitExpectedConditions一起诱导为ElementToBeClickable,并且可以使用以下任一解决方案:

  • CssSelector : 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 : 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. 确保邮政编码的最大长度仅为5位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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