【问题标题】:Can't find input element by xpath无法通过 xpath 找到输入元素
【发布时间】:2019-11-19 21:17:45
【问题描述】:

我正在尝试通过 xpath 获取输入元素,但返回语句为 false。

当我尝试获取输入的唯一同级标签时,它返回 true。我也尝试通过输入输入[@type="checkbox"] 来获取它,但它也失败了。

try {
    var select2 = wait.until(pkg.ExpectedConditions.visibilityOfElementLocated(pkg.By.xpath('//div[@class="arcHier arcVert arcTree"]/ol/li/ul/li[3]/div[@class="dragNode"]/label')))

    WDS.log.info('select2 found')

    var select3 = wait.until(pkg.ExpectedConditions.visibilityOfElementLocated(pkg.By.xpath('//div[@class="arcHier arcVert arcTree"]/ol/li/ul/li[3]/div[@class="dragNode"]/input')))

    WDS.log.info('select3 found')
}
catch (err) {
     WDS.log.error('item not found')
}

HTML 结构:

<li id="4_2_1_292719_8381e915e3af18b6f1f672da1ee582ef_LI" style="line-height:19px;border-color:#a0a0a0;color:#000000;" class="LI_1_2">
    <label class="arcSelectable" style="height:19px;">
        <input type="checkbox" id="4_2_1_292719_8381e915e3af18b6f1f672da1ee582ef_X" class="arcHpCbox" checked="checked">
        <span class="arcMenuCheckbox"></span>
    </label>
    <div class="dragNode">
        <input type="checkbox" id="4_2_1_292719_8381e915e3af18b6f1f672da1ee582ef" style="margin-top:3px;">
        <label draggable="false" for="4_2_1_292719_8381e915e3af18b6f1f672da1ee582ef" style="height:19px;">
            <span class="SPAN_1_2" style=""></span>
0001 Training_Test_Company
        </label>
    </div>
</li>

日志文件:

  1. 2019-07-10 10:55:33,187 信息 c.g.j.p.w.s.WebDriverSampler: select2 找到了
  2. 2019-07-10 10:55:43,310 错误 c.g.j.p.w.s.WebDriverSampler:项目不 找到了

【问题讨论】:

  • 请阅读为什么screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
  • 你能在开发工具中检查输入节点有多少条目吗?
  • @cruisepandey 有 116 个输入元素
  • 我想我们需要更多的 HTML 代码来找到 xpath。

标签: javascript selenium selenium-webdriver jmeter automated-tests


【解决方案1】:

输入元素可能是一个隐藏的复选框。您可以尝试使用presenceOfElementLocated 而不是visibilityOfElementLocated,如下所示。

try {
    var select2 = wait.until(pkg.ExpectedConditions.visibilityOfElementLocated(pkg.By.xpath('//div[@class="arcHier arcVert arcTree"]/ol/li/ul/li[3]/div[@class="dragNode"]/label')))

    WDS.log.info('select2 found')

    var select3 = wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.xpath('//div[@class="arcHier arcVert arcTree"]/ol/li/ul/li[3]/div[@class="dragNode"]/input')))

    WDS.log.info('select3 found')
}
catch (err) {
     WDS.log.error('item not found')
}

【讨论】:

  • 我不知道这是怎么可能的,因为该元素没有隐藏,但它解决了问题。谢谢你,你为我节省了很多时间。祝你好运。
  • 那些 div 有不同的类,所以如果你查看 css 你可能会看到可见性选项...“dragNode”
猜你喜欢
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 2022-07-05
  • 2021-01-13
  • 1970-01-01
  • 2017-07-19
  • 2018-09-20
  • 1970-01-01
相关资源
最近更新 更多