【问题标题】:Unable to indentify the element in selenium C#无法识别硒 C# 中的元素
【发布时间】:2015-08-27 07:03:50
【问题描述】:

我正在自动化其中一个商业应用程序,当我尝试使用 CSS、Xpath、ID 识别元素时,该元素被识别并在 Firebug 中仅显示为一个匹配节点,但是当我将该元素用于测试用例时自动化。我看到以下错误

“OpenQA.Selenium.NoSuchElementException”类型的异常发生在 WebDriver.dll 中,但未在用户代码中处理

请在下面找到应用程序的 html 代码

<div id="QueryOption1" class="SelectedDiv" xmlns:local="#local-functions" xmlns:msxsl="urn:schemas-microsoft-com:xslt" name="QueryOptions">
<span>
<div>
<div class="leftSide">Subscriber ID:       </div>
<input id="Subscriber_ID" class="floatLeft" type="text" name="InsuranceNum" size="15"/>
</div>

以下是我尝试过的选项

By.id("Subscriber_First_Name")

By.CssSelector("#QueryOption1 > div > #Subscriber_Last_Name")

By.Xpath("//input[@id='Subscriber_First_Name']")

By.CssSelector("input[id=Subscriber_First_Name][type=text]")

它们都不适合我将值传递到文本框中。请帮助

【问题讨论】:

  • 你能分享你用来与元素交互的完整代码吗?
  • driver.FindElement(ObtainEntities.Subscriber_First_Name).SendKeys("Fred");

标签: c# selenium-webdriver


【解决方案1】:

你可以使用 IJavaScriptExecuter 来获取它

 IJavaScriptExecutor jsExc = browser as IJavaScriptExecutor;
string textValue = jsExc.ExecuteScript("return document.getElementById('Subscriber_ID').value;").ToString();

【讨论】:

  • 对不起,请找到 HTML 代码
    订阅者名字:
  • 没有理由使用IJavaScriptExecutor。您应该能够使用“正常”方法来获取元素。
  • 我尝试了所有元素定位器,但它不起作用。您能否分享一下确切的 IJavaScriptExecutor 代码以在 UI 中的 webElement(订阅者名字)上传递值
  • IJavaScriptExecutor jsExc = browserVariable 作为 IJavaScriptExecutor; string textValue = jsExc.ExecuteScript("return document.getElementById('Subscriber_First_Name').value;").ToString();这就是你如何得到它。 btw = 你必须用你的浏览器变量名来改变 browserVariable
  • 我添加了代码,但它仍然没有工作。 IJavaScriptExecutor jsExc = _driver 作为 IJavaScriptExecutor; string textValue = jsExc.ExecuteScript("return document.getElementById('Subscriber_First_Name').value;").ToString();
【解决方案2】:

您为什么使用 id 作为:- Subscriber_First_Name 而在您上面的 HTML 中“id”是:-

By.id("QueryOption1")   // for first div
By.id("Subscriber_ID")   //for input tag

By.xpath("//input[@id='Subscriber_ID']")

或者您也可以使用名称定位器

By.name("QueryOptions")
By.name("InsuranceNum")

或者可以使用 JavascriptExecutor 设置值

JavascriptExecutor jse = (JavascriptExecutor)driver; 
jse.executeScript("$('#Subscriber_ID').attr('value','user')");

【讨论】:

    【解决方案3】:

    我很困惑为什么您提供的 HTML 将 INPUT 的 id 显示为 Subscriber_ID 并且您在代码示例中使用了 Subscriber_First_Name。这可能是问题所在,但您没有提供您正在使用的实际代码,所以我不确定。

    试试这个

    driver.FindElement(By.Id("Subscriber_ID")).SendKeys("some text goes into the text box");
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签