簡體   English   中英

如何通過Selenium和C#對輸入元素調用SendKeys()

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

基本上,我想將鍵發送到輸入元素。 我嘗試使用SendKeys()方法,但它不發送使用的值,而是使用輸入框的上一個value屬性。 而且,使用SendKeys()ExecuteScript()方法不會更改輸入元素的value屬性。 以下是我嘗試過但到目前為止仍無法正常工作的代碼段:

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

這是我需要輸入文本到的輸入元素:

<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>

如果你們需要檢查克萊斯勒,這是我要爬的網站。 我希望我已經正確解釋了我的問題。 任何幫助表示贊賞。

在功能“ WaitUntilElementIsPresent”中,您需要檢查應用於webdriver的預期條件。 以下是類似的代碼。 您可以使用“ 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;
        }
    }

要調用SendKeys()您需要將WebDriverWaitExpectedConditions一起誘導為ElementToBeClickable,並且可以使用以下任一解決方案:

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

注意事項

  • 確保郵政編碼僅包含數字。
  • 確保郵政編碼的最大長度僅為5位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM