按ID找不到元素-硒

时间:2018-08-23 14:00:55

标签: c# selenium selenium-webdriver webdriver

尽管使用id,但我找不到输入元素。我已经尝试过CssSelectorXpathId。为什么不能这样处理这个元素?

我尝试过:

By id = By.Id("First name:input");
By xpath = By.Xpath("//*[@id=\\"First name:input\\"]");
By selector = By.CssSelector("#First\\ name\\:input");

我使用explict wait等待输入元素。

输入元素:

<div class="controlPaddingWrapper">
<label id="First name:">First name:</label> 
<input ng-attr-id="{{ c.Prompt+ 'input' }}" next-focus="" focus-if="true" class="k-textbox ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-pattern flow-required ng-touched" pattern=".+$" ng-model="c.Value" ng-class="{'flow-required': isRequired(c)}" required="" style="width: 100%;" ng-change="onChange(c)" id="First name:input" type="text">
</div>

4 个答案:

答案 0 :(得分:0)

HTML The id Attribute

仅尝试:By id = By.Id("First name");

答案 1 :(得分:0)

您可以使用:

By xpath = By.Xpath("//input[@id='First name:input']");
By selector = By.CssSelector("input[id='First name:input']");

答案 2 :(得分:0)

根据 HTML ,您共享了在所需元素上调用Click() / SendKeys()的功能,因为该元素是您需要指示的Angular元素 WebDriverWait ,使元素可点击,您可以使用以下任一解决方案:

  • CssSelector

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.k-textbox.ng-pristine.ng-empty.ng-invalid.ng-invalid-required.ng-valid-pattern.flow-required.ng-touched[id*='First']"))).Click();
    
  • XPath

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='k-textbox ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-pattern flow-required ng-touched' and @id=\"First name:input\"]"))).Click();
    

答案 3 :(得分:0)

尝试一下:

  1. 最好更改ID的空格<label id="First_name">
  2. 如果您想按ID进行搜索,请尝试以下操作:By.id("First_name")
  3. 如果您希望通过XPath:By.Xpath("//*[@id='First_name']")By.Xpath("//label[@id='First_name']")By.Xpath("//label[contains(text(), 'First name:')]") 有关Xpath的参考,您可以在这里https://www.guru99.com/xpath-selenium.html

抱歉,CssSelector从未使用

<div class="controlPaddingWrapper">
<label id="First name:">First name:</label> 
<input ng-attr-id="{{ c.Prompt+ 'input' }}" next-focus="" focus-if="true" class="k-textbox ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-pattern flow-required ng-touched" pattern=".+$" ng-model="c.Value" ng-class="{'flow-required': isRequired(c)}" required="" style="width: 100%;" ng-change="onChange(c)" id="First name:input" type="text">
</div>