[英]How to select visible element using XPath or CSS?
I want to select the apply button in the following code. 我想在以下代码中选择“应用”按钮。 There are two buttons and only one button is visible. 有两个按钮,只有一个按钮可见。
//input[@value='Apply' and @id='btn' and @name='btn' and not(ancestor::td[contains(@style,'display:none')])]
I have written above XPath to select the visible one but in web driver it says unable to access the element. 我已经在XPath上方编写了选择可见的XPath,但是在Web驱动程序中它说无法访问该元素。 (browser - IE8) (浏览器-IE8)
<table class="ColumnTable" cellspacing="0">
<tbody>
<tr>
<td>
<div id="dashboard~120" class="Section" style="" headeron="" minimized="false" rendered="false">
<table class="SectionT" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style=" display:none;">
<div id="dashboard~Contents" style="">
<table style="width:100%">
<tbody>
<tr height="100%">
<td class="EItem" valign="TOP" align="CENTER" colspan="2" style="">
<div id="EmbedViewd" reloadinline="">
<div id="NavDone" style="display:;">
<div id="Result" result="Prompt">
<table class="ViewTable" cellspacing="0">
<tbody>
<tr>
<td>
<div id="newLayout">
<form style="margin: 0;" method="post" action="javascript:void(null);">
<div style="">
<table class="PromptView" style="">
<tbody>
<tr>
<td class="ButtonsCell">
<input id="btn" class="button" type="button" tabindex="0" value="Apply" name="btn" style="background-color: rgb(240, 240, 240);">
</td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td>
<div id="dashboard~121" class="Section" style="" headeron="true" minimized="false" rendered="false">
<table class="SectionT" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<div id="dashboard~Contents" style="">
<table class="SectionTD" style="width:100%; border-top:none;">
<tbody>
<tr height="100%">
<td class="EItem" valign="TOP" align="CENTER" colspan="2" style="">
<div id="EmbedViewd" reloadinline="">
<div id="NavDone" style="display:;">
<div id="Result" result="Prompt">
<table class="ViewTable" cellspacing="0">
<tbody>
<tr>
<td>
<div id="newLayout">
<form style="margin: 0;" method="post" action="javascript:void(null);">
<div style="">
<table class="PromptView" style="">
<tbody>
<tr>
<td class="ButtonsCell">
<input id="btn" class="button" type="button" tabindex="0" value="Apply" name="btn" style="background-color: rgb(240, 240, 240);">
</td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
My question is there anyway other work around for this issue. 我的问题是针对此问题还有其他解决方法。 I think there are plenty of ways to write the above xpath am i right? 我认为有很多方法可以编写上述xpath,对吗?
You could try the following in case this is a Selenium issue: 如果这是硒问题,可以尝试以下方法:
//input[@value='Apply'][@id='btn'][@name='btn']
[not(ancestor::td[contains(@style,'display:none')])]
It's the same expression with the same result, but as mentioned here Xpath does not work with Selenium it's possible that Selenium has an issue with evaluating and
in XPath. 这是相同的表达具有相同的结果,但这里所说的XPath不与硒的工作有可能是硒有一些问题与评估and
在XPath中。
Another issue I just want to mention is that you shoudn't use the same id
for multiple elements, id
s should be unique. 我只想提及的另一个问题是,您不应对多个元素使用相同的id
, id
应该是唯一的。 Otherwise your HTML is not valid. 否则,您的HTML无效。 When you change the ids to unique values, it'd be possible to reduce the XPath match conditions. 当您将ID更改为唯一值时,可以减少XPath匹配条件。
I am a bit confused about your question. 我对你的问题有点困惑。
If you are trying to select the button in order to modify its style through CSS, just create the appropriate entry in your CSS file and an ID for the HTML element (the button you need). 如果您试图选择按钮以通过CSS修改其样式,只需在CSS文件中创建适当的条目以及HTML元素的ID(您需要的按钮)即可。
Example in W3: Select HTML element by ID W3中的示例: 通过ID选择HTML元素
Selecting an element using xpath: 使用xpath选择一个元素:
Selecting the first element: 选择第一个元素:
//div[@id='dashboard~120']descendant::input[@id='btn'].Click; //div[@id='dashboard~120']descendant::input[@id='btn'].Click;
Selecting the second element: 选择第二个元素:
//div[@id='dashboard~121']descendant::input[@id='btn'].Click; //div[@id='dashboard~121']descendant::input[@id='btn'].Click;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.