【问题标题】:How to invoke SendKeys() to the input element through Selenium and C#如何通过 Selenium 和 C# 调用 SendKeys() 到输入元素
【发布时间】:2019-08-02 14:11:40
【问题描述】:

基本上,我想将键发送到输入元素。我尝试使用SendKeys() 方法,但它不发送我使用的值,而是使用输入框的前一个值属性。此外,使用SendKeys()ExecuteScript() 方法不会改变输入元素的值属性。以下是我尝试过但到目前为止还没有工作的代码片段:

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

如果你们需要检查Chrysler,这是我正在抓取的网站。 我希望我已经正确解释了我的问题。任何帮助表示赞赏。

【问题讨论】:

  • 在不同站点上测试相同代码后发现这是通过克莱斯勒站点进行网络爬网时出现的问题。但是,我仍在寻找一种方法来完成这项任务。

标签: c# selenium xpath css-selectors webdriverwait


【解决方案1】:

在“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;
        }
    }

【讨论】:

  • 感谢您的回复,但这只会抛出异常消息。请参阅@DebanjanB 答案下的 cmets
【解决方案2】:

要调用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位。

【讨论】:

  • 感谢您的回复,但它无法正常工作,因为它会抛出 OpenQA.Selenium.WebDriverTimeoutException: 'Timed out after 20 seconds' 。但是,zipcodeInput.Clear();zipcodeInput.Click(); 方法之前有效(忘记提及)。再说一次,这个条件不应该失败,因为 Clear()Click() 方法在它上面起作用。
  • 它仍然会从WebDriverWait 抛出WebDriverTimeoutException,所以它不会到达下面的代码。
  • @LordDraagon 查看我更新的答案并让我知道状态
  • 仍然抛出相同的异常。但是,我使用了这个` js.ExecuteScript("document.getElementById('fad-dealer-searchbar-search-textbox').children[0].children[1].children[0].value = "+ zipcode +" ");` 并遍历选择输入框并设置值。现在它改变了值但是当我调用回车键时它变回00002
猜你喜欢
  • 2023-02-24
  • 2021-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-22
  • 2020-06-15
  • 2016-11-25
  • 1970-01-01
相关资源
最近更新 更多