首页 > 解决方案 > 通过 cssSelector 查找元素

问题描述

我对 Selenium 很陌生,我正在征求您的建议。

我有以下html:

<div class="order">
<a style="float: left;" href="#137674" class="voidCmn ui-button ui-corner-all ui-widget" role="button">Void</a>
<a style="float: left;" href="printcmn.php?Id=137674" target="_blank" class="ui-button ui-corner-all ui-widget" role="button"> Print </a>
<a style="float: left;" href="section_a.php?cmnId=137674&amp;origin=viewreferral.php" class="ui-button ui-corner-all ui-widget" role="button"> Edit </a>
</div>

在那种情况下,我想使用 cssSelector 而不是 xpath。

我查找网页元素按钮编辑的代码(见上文):

Driver.findElement(By.cssSelector("a.ui-button.ui-corner-all.ui-widget[role='button'][href^='viewreferral.php']")); 

但是,我收到一个错误:

no such element: Unable to locate element: {"method":"css selector","selector":"a.ui-button.ui-corner-all.ui-widget[role='button'][href^='viewreferral.php']"}

您能否回顾一下并告诉我出了什么问题。

标签: htmlcssselenium-webdriver

解决方案


请使用下面给定的 css 选择器,它应该可以工作:

 a.ui-button.ui-corner-all.ui-widget[role='button'][href*='viewreferral.php']

您使用了错误的运算符来检查包含。你用过:^但它应该是*


推荐阅读