gpt4 book ai didi

javascript - 如何使用 XPath 或 CSS 选择可见元素?

转载 作者:行者123 更新时间:2023-11-29 18:12:16 26 4
gpt4 key购买 nike

我想在下面的代码中选择应用按钮。有两个按钮,只有一个按钮可见。

//input[@value='Apply' and @id='btn' and @name='btn' and not(ancestor::td[contains(@style,'display:none')])]

我在 XPath 上面写了选择可见的,但是在网络驱动程序中它说无法访问该元素。 (浏览器 - 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>

我的问题是无论如何还有其他解决方法可以解决这个问题。我认为有很多方法可以编写上面的 xpath,对吗?

最佳答案

如果这是 Selenium 问题,您可以尝试以下操作:

//input[@value='Apply'][@id='btn'][@name='btn']  
[not(ancestor::td[contains(@style,'display:none')])]

这是具有相同结果的相同表达式,但如此处所述Xpath does not work with Selenium Selenium 可能在评估 XPath 中的 时遇到问题。

我想提的另一个问题是,你不应该对多个元素使用相同的 idid 应该是唯一的。否则您的 HTML 无效。当您将 ID 更改为唯一值时,可以减少 XPath 匹配条件。

关于javascript - 如何使用 XPath 或 CSS 选择可见元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com