【问题标题】:c# selenium chrome-webdrive clicking button using class and titlec# selenium chrome-webdriver 点击按钮使用类和标题
【发布时间】:2017-10-07 01:29:02
【问题描述】:

我只想点击我页面中的按钮。 html 代码看起来像:

<tr ng-repeat="row in rowCollection" ng-class="{ &quot;error-row&quot;: row.errorMessage }" ng-style="vm.getColor(row)" class="ng-scope" style="background: rgb(255, 242, 255) none repeat scroll 0% 0%;">
    <td class="ng-binding">Wylaczenie nadan RDF</td><td class="ng-binding">WAITING_FOR_NOTIFICATION</td>
    <td>
        <a href="" ng-click="vm.showProcessDiagram(row.executor)" class="ng-binding">rfsSendingExecutor</a>
    </td>
    <td class="ng-binding">2017-09-06 11:14:12</td><td class="ng-binding">2017-09-06 11:14:13</td>
    <td has-role="REQUEST" class="text-center">
    <!-- ngIf: row.inXml || row.outXml -->
    <button ng-if="row.inXml || row.outXml" ng-click="vm.showXml(row)" title="Show" class="btn btn-xs ng-scope"><span class="fa fa-code"></span></button>
    <!-- end ngIf: row.inXml || row.outXml -->
    </td>
    <td has-role="ERROR" class="text-center"><button ng-show="row.errorMessage" ng-click="vm.showError(row.errorMessage)" title="Show" class="btn btn-xs ng-hide"><span class="fa fa-search"></span></button></td>
    <td class="text-center">
    <button ng-show="vm.enableCancel(row)" ng-click="vm.cancelTask(row.workItemId)" title="Cancel" class="btn btn-xs ng-hide">
        <span class="fa fa-ban text-warning"></span>
    </button> 
    <button ng-show="vm.enableRepeat(row)" ng-click="vm.repeatTask(row.id)" title="Repeat" class="btn btn-xs ng-hide">
        <span class="fa fa-refresh text-success"></span>
    </button> 
    <button ng-show="vm.enableRepeat(row)" ng-click="vm.repeatTaskWithParams(row.id)" title="Repeat with parameters" class="btn btn-xs ng-hide">
        <span class="fa fa-refresh text-warning"></span>
    </button>
    <button ng-show="vm.enableSkip(row)" ng-click="vm.skipTask(row.workItemId)" title="Skip" class="btn btn-xs">
        <span class="fa fa-angle-double-right text-success"></span>
    </button>
    </td>
</tr>

我只想点击这个按钮:

<button ng-show="vm.enableSkip(row)" ng-click="vm.skipTask(row.workItemId)" title="Skip" class="btn btn-xs">
        <span class="fa fa-angle-double-right text-success"></span>

我已经阅读了 xpath 教程并查看了许多其他帖子和论坛。我不确定我错过了什么。我只是想像这样通过 xpath 找到以下元素:

button_to_click= findElement(By.xpath("//button[@title='Skip']"));

但它不起作用。 问题:为什么它不能只按标题工作? 我尝试另一种方式并这样做:

 button_to_click= findElement(By.xpath("//button[@class='btn btn-xs']"));

而且效果很好,但是当我在这个类中有 3 或 4 个元素时,它只是按了错误的按钮。 我怎样才能准确地按下这个按钮有人可以帮助我吗?
也许我应该尝试这样的事情?

button_to_click= findElement(By.xpath("//button[@class='btn btn-xs']//button[@title='Skip']"));

为什么它不能只按标题工作?我怎样才能做得更好?新手请耐心等待。

编辑 1 我添加了更多代码,因为您想知道我在做什么。 : 这段代码运行良好:

driver = new ChromeDriver();
driver.url ="http://mypage.com"
button_to_click= findElement(By.xpath("//button[@class='btn btn-xs']")).Click();

而且这段代码不起作用:

driver = new ChromeDriver();
driver.url ="http://mypage.com"
button_to_click= findElement(By.xpath("//button[@title='Skip']")).Click();

编辑 2 我会给你一个示例页面进行测试。您只需下载 html 文件并在浏览器中打开即可。Html page file 我们现在想要做什么? 如果您运行此 html 文件,您将看到所有页面。 现在我们想在屏幕上点击这个按钮: 当你点击这个按钮后,你会看到下面的点击计数器:像这样: 有谁知道如何点击它?我尝试了几种方法,但仍然找不到解决方案。请帮忙。

编辑 3

我也尝试过:-但它也不起作用

drive.FindElement(By.XPath("//tr[class='ng-scope']/td[text()='Wylaczenie nadan RDF'] and button[@title='Skip'']]")).Click();

【问题讨论】:

  • 注意到按钮位于表格行中。页面上是否有多个按钮实例。你也可以展示更多你的 c# 代码来了解你想要做什么
  • 我觉得奇怪的是 xpath("//button[@title='Skip']") 不起作用。并通过反转: xpath('//button[@title="Skip"]') ?我知道它是一样的,但也许??
  • 是的,我在页面上有多个按钮实例。我正在尝试单击包含 title='Skip' 的按钮。
  • 请检查主帖中的 EDIT 1 我添加了更多代码,例如我也尝试使用 - ng-click="vm.skipTask(row.workItemId) 和 ng-show="vm.enableSkip(row )" 但不像标题那样工作。
  • 我不认为“标题”标签是问题所在。因为你的 xpath 是正确的。例如:php.wekeepcoding.com/article/18943879/…, //*[@title="xpath"] 有效

标签: c# selenium xpath chrome-web-driver


【解决方案1】:

根据您的问题,这行代码有效:

button_to_click= findElement(By.xpath("//button[@class='btn btn-xs']")).Click();

这行代码不起作用:

button_to_click= findElement(By.xpath("//button[@title='Skip']")).Click();

解释:

看看HTML DOM,很明显WebApplication使用了很多JavaScriptAjax Calls。因此是属性,例如ng-repeatng-class 等具有动态值,例如{ &amp;quot;error-row&amp;quot;: row.errorMessage }vm.showError(row.errorMessage) 等。因此很难使用这些值/属性来构造xpathCSSselector

如果xpath 唯一标识了我们感兴趣的特定元素,则将xpath 用作//button[@title='Skip'] 应该可以工作。但由于它没有发生,我怀疑可能有多个元素与 xpath 匹配,其中一些元素可能被禁用/隐藏。所以,xpath 使用 title 属性作为 Skip FAILED

xpath 用作//button[@class='btn btn-xs'] 不会失败,因为这里我们考虑了在CSSselectorxpath 中广泛使用的class 属性,它映射到查询选择器/查询选择器全部。因此,此选项更可靠且效果更好。

更新:

虽然将xpath 用作//button[@class='btn btn-xs'] 可以为您工作而不会失败,但我不确定您为什么要避免它。关于您在评论中提到的xpath,由于您在使用&lt;button&gt; 标签的搜索中获得了很多粒度,因此似乎没有必要引用任何父节点,例如tr[text()='Wylaczenie nadan RDF']。如果xpath as //button[@class='btn btn-xs'] 不能唯一标识元素,您可以考虑将classtitle 属性组合起来,如下所示:

button_to_click= findElement(By.xpath("//button[@class='btn btn-xs' and @title='Skip']")).Click();

【讨论】:

  • 你是对的。你想写它应该看起来点击那个按钮吗? //tr[text()='Wylaczenie nadan RDF']/button[@title='Skip'] 够了吗?
  • 我认为这是非常好的回答。让我问最后一件事。但是当我只想做这样的事情时: button_to_click= findElement(By.xpath("//tr[text()='Wylaczenie nadan RDF']/button[@class='btn btn-xs' and @title= '跳过']")).Click();可以吗,应该可以正常工作吗?
  • 好的,我检查一下。这很好用: button_to_click= findElement(By.xpath("//button[@class='btn btn-xs' and @title='Skip']")).Click(); .但是,当我尝试单击按钮时,正如我在编辑 2 中所说的那样: button_to_click= findElement(By.xpath("//tr[text()='Wylaczenie nadan RDF']/button[@class='btn btn- xs' 和 @title='Skip']")).Click(); - 它不起作用,你能帮我点击这个正确的按钮吗?您的帖子非常好,但它点击了本课程中的第一个按钮,而这个标题不是我想要的。你可以在这个网站上查看我在主帖中发布的内容,它不起作用。
  • 也检查 EDIT 3
  • 你能帮我解决另一个话题吗?我看你知道很多。非常感谢。 stackoverflow.com/questions/46611317/…
【解决方案2】:

我很好奇您是否在 dom 中有一个更高的元素,它具有与您尝试选择的 XPath 相同的属性和 html 元素。让我们用我们的 XPath 变得超级具体,并加入一些条件。试一试这家伙:

//button[@ng-show='vm.enableSkip(row)' and @title='Skip' and @class='btn btn-xs']

如果您需要添加更多标识符,请继续将它们扔在那里。拥有非常具体的 XPath 并没有错。

您的按钮看起来也位于表格行中。如果包含按钮的 tr 元素中有任何独特的东西,那么您绝对应该在按钮之前将其扔到 xpath 中,并且您不必担心它单击前一个 tr 中的按钮,这样做您就知道了对于 XPath 按钮部分的独特性,除了 @title='Skip' 之外不需要其他任何东西。

例如...

//tr[@attribute='uniqueTRValue']/button[@title='Skip']

【讨论】:

  • 你能看看我的例子,并按照我说的那样尝试为这个按钮设置路径吗?唯一的是“Wylaczenie nadan RDF”,但在单击按钮类之前属于子类
  • //tr[text()='Wylaczenie nadan RDF']/button[@title='Skip']
  • 当我回到家时我会检查这个并编码然后我在这里重播
  • 不起作用 - //tr[text()='Wylaczenie nadan RDF']/button[@title='Skip']
猜你喜欢
  • 2020-03-16
  • 1970-01-01
  • 2020-09-04
  • 2020-04-12
  • 1970-01-01
  • 2018-03-16
  • 1970-01-01
  • 2020-07-10
  • 2015-04-25
相关资源
最近更新 更多