【问题标题】:Unable to locate element in Selenium using correct xpath无法使用正确的 xpath 在 Selenium 中定位元素
【发布时间】:2016-09-17 13:22:58
【问题描述】:

我的 AUT 有不同的选项卡来测试和验证各种 WebElement,它们具有几乎相同的属性,出现在这些选项卡上。 我需要验证下拉搜索框并验证下拉列表中列出的选项。 我的脚本找不到这个下拉搜索框,而 xpath 在 firebug 中工作得很好并且可以识别搜索框。

显示的错误是:

线程 "main" org.openqa.selenium.NoSuchElementException 中的异常:无法定位元素:{"method":"xpath","selector":"//div[@class='btn-group']/选择[@id='selectBS']"}

我的脚本如下:

if(Driver.findElement(By.xpath("//div[@class='btn-group']/select[@id='selectBS']")).isDisplayed());
{
    System.out.println(" drop down box is displayed on the View Bill screen");
}

HTML如下:

<div class="col-md-5 col-sm-6 col-xs-12">
   <div class="row panel panel-default panel-heading">
      <div class="col-sm-4 col-xs-12">
         <div class="dropdown">
            <div class="btn-group">
               <select id="selectBS" class="ng-pristine ng-valid ng-touched" ng-change="getBasicSearchSelectedOption()" ng-model="basicSearchSelectedOption" ng-options="option.text for option in basicSearchoptions track by option.index">
                     <option value="0" selected="selected" label="Search For..">Search For..</option>
                     <option value="1" label="Invoice Group Number">Invoice Group Number</option>
                     <option value="2" label="Account Number">Account Number</option>
                     <option value="3" label="Account Name">Account Name</option>
                     <option value="4" label="Type">Type</option>
               </select>
           </div>
       </div>
   </div> 

请让我知道可能出了什么问题,以及为什么 Selenium 无法使用 xpath 定位元素,而它在 xpath 中工作正常。 另外,如果我的 xpath 不正确,请告诉我。

【问题讨论】:

  • 尝试添加几秒钟的隐式等待元素出现在页面上
  • 嗨安德森,谢谢。我已经在脚本的开头包含了隐式等待。 Driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);我还尝试在此 sn-p 之前添加睡眠线程,但没有帮助:try{ Thread.sleep(500); }catch(InterruptedException e){ e.printStackTrace(); }
  • WebDriverWait 等待 = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='btn-group']/select[@id='selectBS']")));
  • 嗨 Andersson / Kishen,这对我也没有帮助。当我重新运行脚本时,它显示以下错误:第一个错误:线程“main”中的异常 org.openqa.selenium.TimeoutException:20 秒后超时等待 By.xpath 定位的元素的可见性://div [@class='btn-group']/select[@id='selectBS'] 第二个错误:无法定位元素:{"method":"xpath","selector":"//div[@class='btn-group ']/select[@id='selectBS']"}.

标签: selenium xpath


【解决方案1】:

检查是否存在,然后执行一些操作以使选项可见并检查选项是否可见。如果这些工作正常,那么检查 isDisplayed 类型为 select 的元素可能会出现问题。

【讨论】:

    猜你喜欢
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2018-10-31
    • 1970-01-01
    相关资源
    最近更新 更多