识别网页selenium上的按钮元素

时间:2016-11-14 06:04:07

标签: java html css selenium

我正试图在我的网页上找到一个讨厌的按钮。我能找到的大多数其他元素,但这个让我头疼。

html是:

<table class="d_FG" role="presentation">
<tbody>
<tr>
<tr id="z_t">
<td class="fct_w" colspan="2">
<div>
<input name="newAttachments_fsid" value="0" type="hidden">
<table id="z_u" class="dcs" role="presentation">
<tbody>
<tr style="border: none;">
<td colspan="3" style="padding-right:0">
<a id="z_v" class="vui-button d2l-button d2l_1_192_930" role="button" tabindex="0" aria-disabled="false">Add a File</a>
<a id="z_w" class="vui-button d2l-button d2l_1_193_372" role="button" tabindex="0" aria-disabled="false">Record Audio</a>
</td>
<td></td>
</tr>
</tbody>
</table>
</div>
 </td>
 </tr>
 </tbody>
 </table>

我正在尝试找到元素:

<a id="z_v" class="vui-button d2l-button d2l_1_192_930" role="button" tabindex="0" aria-disabled="false">Add a File</a>

我尝试了各种方法,例如:

    public void add_attachment(){
    driver.switchTo().defaultContent();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    driver.findElement(By.id("z_v"))).click();

}

但是无法管理它。始终得到不可见的消息或点击其他元素。

我尝试使用向下滚动到元素的javascript,但这不起作用。非常感谢任何帮助我的想法

1 个答案:

答案 0 :(得分:1)

您可以尝试使用其他解决方法,例如使用ActionsjavascriptExecutor

WebElement btn = driver.findElement(By.id("z_v")));
Actions action = new Actions(driver);
action.click(btn).build.perform();

JavascriptExecutor js = (JavascriptExecutor)driver;
Js.executeScript("arguments[0].click()",btn);