【问题标题】:Selenium NoSuchElementException on input field输入字段上的 Selenium NoSuchElementException
【发布时间】:2019-02-08 23:53:14
【问题描述】:

我尝试通过 Java Selenium 将密钥发送到输入字段。我每次都得到 NoSuchElementException。我还尝试了此解决方案中的所有内容:NoSuchElementExeption, selenium unable to locate element。提前致谢!

driver.findElement(By.xpath("//input[@class='pull-left ng-pristine ng-validng-empty ng-touched']")).sendKeys(t + Keys.ENTER);

<input class="pull-left ng-pristine ng-valid ng-empty ng-touched" ng-model="TagInputCtrl.tagInput" uib-typeahead="tagSuggestion for tagSuggestion in TagInputCtrl.getTagSuggestions($viewValue)" select-on-comma="" select-on-whitespace="" select-on-blur="" typeahead-focus-first="false" tag-select="TagInputCtrl.onEnter" tag-select-model="ngModel" sprd-max-input-length="50" ng-show="ngModel.length < TagInputCtrl.validatorOptions.tags.max" ng-focus="TagInputCtrl.focused = true" ng-blur="TagInputCtrl.focused = false" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-4377-3960" style="" type="text"/>

【问题讨论】:

  • xpath 与当前标签不匹配。
  • 抱歉输入字段错误。现在它是正确的。所以xpath是正确的。
  • 如果您在页面加载(在搜索中)时将该 xpath 定位器输入到 chrome 调试器中,它会找到该元素吗?还是它可能找到多个元素?

标签: java selenium xpath css-selectors webdriver


【解决方案1】:

要发送 字符序列,因为所需元素是 Angular 元素,因此您需要诱导 WebDriverWait 以使 元素可点击 您可以使用以下任一解决方案:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.pull-left.ng-pristine.ng-valid.ng-empty.ng-touched[ng-model^='TagInputCtrl']"))).sendKeys("Tim");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='pull-left ng-pristine ng-valid ng-empty ng-touched'][contains(@ng-model,'TagInputCtrl')]"))).sendKeys("Tim");
    

【讨论】:

    【解决方案2】:

    NoSuchElementException 可以有两件事:

    1. 您的定位器不正确。

    如果是这种情况,那么您可以尝试使用此 xpath:

    //input[contains(@class,'pull-left ng-pristine ng-valid ng-empty ng-touched') and @ng-model='TagInputCtrl.tagInput' and @ng-focus='TagInputCtrl.focused = true']  
    

    您的网页已与 Angular 集成。所以提供的 xpath 应该可以工作。

    1. 您的输入标签可能在 iframe/frame/frameset 中。

    如果是这种情况,那么我建议您将驱动程序的焦点切换到特定的 iframe,以与期望元素进行交互。

    切换你可以试试这个代码:

    driver.switchTo().frame(name_or_id)  
    

    iframe标签一般包含name或id属性,如果两者都不可用,可以继续

    driver.switchTo().frame(index)  
    

    driver.switchTo().frame(iframe_element)  
    

    这里,iframe_element 是一个网络元素。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2021-04-25
      • 2020-12-27
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多