【问题标题】:C# Selenium SendKeys not working even with Javascript executorC# Selenium SendKeys 即使使用 Javascript 执行程序也无法正常工作
【发布时间】:2021-01-06 11:59:03
【问题描述】:

测试找到了元素,因为它没有失败但无法发送参数。

尝试以下:

driver.FindElement(By.XPath("//input[@type='search']")).Clear();
driver.FindElement(By.XPath("//input[@type='search']")).Click();

IWebElement wb = driver.FindElement(By.XPath("//input[@type='search']"));
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("arguments[0].value='QA Test Automation Developer';", wb);

【问题讨论】:

  • wb.SendKeys("你想要的任何文本");什么都不做?

标签: c# selenium xpath css-selectors webdriverwait


【解决方案1】:

由于它是<input> 标签,您必须为所需的ElementToBeClickable() 诱导WebDriverWait,您可以使用以下Locator Strategies 之一:

  • CssSelector:

    IWebElement wb = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input[type='search']")));
    wb.Click();
    wb.Clear();
    wb.SendKeys("QA Test Automation Developer");
    
  • XPath:

    IWebElement wb = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@type='search']")));
    wb.Click();
    wb.Clear();
    wb.SendKeys("QA Test Automation Developer");
    

使用IJavaScriptExecutor

  • 使用setAttribute()innerHTML

    IWebElement wb = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input[type='search']")));
    IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
    jse.ExecuteScript("arguments[0].setAttribute('innerHTML','QA Test Automation Developer')", wb);
    
  • 使用setAttribute()textContext

    IWebElement wb = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input[type='search']")));
    IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
    jse.ExecuteScript("arguments[0].setAttribute('textContext','QA Test Automation Developer')", wb);
    

参考

您可以在以下位置找到一些相关讨论:

【讨论】:

  • 尝试了以上所有选项,但仍然不起作用。如果您可以签出,这是我试图自动化的链接(jobs.labcorp.com/).There 将是关键字搜索并提交似乎找不到元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 2014-11-28
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多