【问题标题】:Xpath to click on Eye IconXpath 点击 Eye Icon
【发布时间】:2020-10-28 01:57:52
【问题描述】:

在我的应用程序中,我必须对 Eye 图标执行单击操作才能查看文档,但我无法生成正确的 xpath 来定位该图标。

以下是 HTML 代码的图像。我无法复制粘贴代码,因此附上图像。

    <div title="" class="dragItem row " id="DocSelected-1" draggable="true" type="11" data-content="Salary Slips" data-draggable="item" document-required="1" state="notVerified" accountid="0">
    <span class="col-xs-10 dl-no-padding">
Salary Slips                                    
    </span>
    <i class="document-icon-eye dl-padding-right-10 pull-right" aria-hidden="true" onclick="openValidDoc('DocSelected-1')">
    </i>
    </div>

以下是尝试过的代码,但我没有这样的元素错误。

IWebElement salaryeyeicon = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span/span/i"));
            salaryeyeicon.Click(); 

(OR)

 IWebElement salaryeyeicon = driver.FindElement(By.XPath("//div[@id='DocSelected-1']//span[@class='col-xs-10 dl-no-padding']//i[@class='document-icon-eye dl-padding-right-10 pull-right']"));
     salaryeyeicon.Click(); 

请提出正确的定位元素的方法。

【问题讨论】:

  • 有人已经有了。

标签: c# selenium unit-testing testing xpath


【解决方案1】:

您的第一个 xpath 的问题可能是 i 元素没有嵌套在任何 span 元素中。

也许没有必要指定元素的完整路径,因为它的类 document-icon-eye 在您的具体场景中或多或少是足够的标识符。你可以使用这样的东西:

//div[@id='DocSelected-1']//i[contains(@class, 'document-icon-eye')]

我建议您使用浏览器开发者工具中内置的 xpath 搜索工具。将候选 xpath 粘贴到搜索字段(元素视图中的 Ctrl + F)将快速显示所提供的 xpath 是否可以正常工作。可能这可以为您节省大量时间,避免代码的连续试错编译和执行。

编辑后,您的 xpath 的正确版本将是:

//div/i

//div[@id='DocSelected-1']//i[@class='document-icon-eye dl-padding-right-10 pull-right']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2021-06-09
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多