简体   繁体   English

Selenium Java-尝试单击按钮

[英]Selenium Java - trying to click button

I am trying to click a button that that has the below HTML. 我试图单击具有以下HTML的按钮。

<button title="XXYZ" class="clsBarButton" id="tbCodeBookI10" hidefocus="true"
    onmouseover="changeTBC(this)" onmouseout="changeTBC(this)" onmousedown="changeTBC(this)"
    onmouseup="changeTBC(this)" onclick="OpenReference('i10codebook'); resyncTabs();" type="button">
</button>

I tried the following different lines in my code but all of them throws no such element. 我在代码中尝试了以下不同的行,但所有这些行均未抛出此类元素。

driver.findElement(By.xpath("//button[@id='tbCodeBookI10' and class='clsBarButton2']")).click()

driver.findElement(By.cssSelector("html body.clsBody.clsAJAXBody div#Toolbar
button#tbCodeBookI10.clsBarButton")).click();

This should work just fine 这应该很好

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

If it keeps on failing as you suggest in your comment, check two things, that the HTML is a valid HTML, and that your button is not within an iframe (in which case you would first need to switch to the iframe) 如果您仍然按照注释中的建议继续失败,请检查两件事,即HTML是有效的HTML,并且按钮不在iframe内(在这种情况下,您首先需要切换到iframe)

For switching to iframe the following code should work 要切换到iframe,以下代码应该有效

    driver.switchTo().defaultContent(); 
    WebElement yourIframe = driver.findElement(By.name(*THE NAME OF YOUR IFRAME*));
    driver.switchTo().frame(yourIframe);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM