[英]Unable to click the 'button' in selenium webdriver (using Java)
[英]Unable to click a button in a table using webdriver
我需要双击表中的元素(名为用户)。 尝试使用xpath,但出现错误消息“无法定位元素”。 我也尝试过使用className,linkText,tagName。 任何人都可以请这个。
这是一段HTML代码
<div id="gridpanel-1064" class="x-panel filesystem-filegrid x-grid-with-row-lines x-border-item x-box-item x-panel-default x-grid" style="right: auto; left: 305px; top: 0px; margin: 0px; width: 1373px; height: 343px;">
<div id="gridpanel-1064_header" class="x-panel-header x-docked x-panel-header-default x-docked-top x-panel-header-docked-top x-panel-header-default-docked-top x-layout-fit" style="width: 1373px; right: auto; left: 0px; top: 0px;">
<div id="toolbar-1075" class="x-toolbar x-docked x-toolbar-default x-docked-top x-toolbar-docked-top x-toolbar-default-docked-top x-box-layout-ct" style="width: 1373px; right: auto; left: 0px; top: 32px;">
<div id="headercontainer-1065" class="x-grid-header-ct x-docked x-grid-header-ct-default x-docked-top x-grid-header-ct-docked-top x-grid-header-ct-default-docked-top x-box-layout-ct" style="border-width: 1px; width: 1373px; right: auto; left: 0px; top: 59px;">
<div id="gridpanel-1064-body" class="x-panel-body x-grid-body x-panel-body-default x-layout-fit x-panel-body-default" style="width: 1373px; left: 0px; height: 226px; top: 84px;">
<div id="gridview-1074" class="x-grid-view x-fit-item x-grid-view-default x-unselectable" style="overflow: auto; margin: 0px; width: 1371px; height: 224px;" tabindex="-1">
<table id="gridview-1074-table" class="x-gridview-1074-table x-grid-table" cellspacing="0" cellpadding="0" border="0" tabindex="-1" style="width: 918px;">
<colgroup>
<colgroup>
<colgroup>
<colgroup>
<colgroup>
<colgroup>
<colgroup>
<colgroup>
<colgroup>
<tbody id="gridview-1074-body">
<tr id="gridview-1074-record-ext-record-23" class="x-grid-row x-grid-data-row" tabindex="-1" data-recordindex="0" data-recordid="ext-record-23" data-boundview="gridview-1074">
<tr id="gridview-1074-record-ext-record-24" class="x-grid-row x-grid-row-alt x-grid-data-row" tabindex="-1" data-recordindex="1" data-recordid="ext-record-24" data-boundview="gridview-1074">
<tr id="gridview-1074-record-ext-record-25" class="x-grid-row x-grid-data-row" tabindex="-1" data-recordindex="2" data-recordid="ext-record-25" data-boundview="gridview-1074">
<tr id="gridview-1074-record-ext-record-26" class="x-grid-row x-grid-row-alt x-grid-data-row" tabindex="-1" data-recordindex="3" data-recordid="ext-record-26" data-boundview="gridview-1074">
<tr id="gridview-1074-record-ext-record-27" class="x-grid-row x-grid-data-row" tabindex="-1" data-recordindex="4" data-recordid="ext-record-27" data-boundview="gridview-1074">
<td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1100 x-grid-cell-first x-unselectable x-grid-cell-special x-grid-cell-row-checker">
<td class="x-grid-cell x-grid-td x-grid-cell-actioncolumn-1066 x-unselectable x-action-col-cell">
<td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1067 x-unselectable ">
<div class="x-grid-cell-inner" style="text-align:left;" unselectable="on">users</div>
</td>
<td class="x-grid-cell x-grid-td x-grid-cell-actioncolumn-1068 x-unselectable x-action-col-cell">
<td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1069 x-unselectable ">
<td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1070 x-unselectable ">
<td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1071 x-unselectable ">
<td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1072 x-unselectable ">
<td class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1073 x-grid-cell-last x-unselectable ">
</tr>
</tbody>
</table>
</div>
</div>
那么您当前的XPath是错误的:
//table/tbody/tr[5]/tr[3]/div
看看它做什么。
它将进入table
,成table
的tbody
,进入第五 tr
(行),那么错误是,它会接着进入第三 tr
是内(行,还记得吗?)
将其与您所拥有的进行比较。
您可能想要:
//table/tbody/tr[5]/td[3]/div
如第5个 tr(行)中的第三个 td
(单元)。
该XPath将选择div,其中包含类为x-grid-cell-inner
的文本users
XPath
//div[contains(., 'users') and contains(@class, 'x-grid-cell-inner')]
查找元素
<div class="x-grid-cell-inner" style="text-align:left;" unselectable="on">users</div>
如果存在多个具有这些属性的div,则需要对XPath应用其他限定符。
希望这个能对您有所帮助...
driver.findElement(By.xpath("//table[@id='gridview-1074-table']/tbody/tr[5]/tr[3]/div")).click();
也许是这样的:
driver.findElement(By.xpath("//div[contains(string(), 'users')]"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.