简体   繁体   English

Selenium Webdriver-无法单击按钮

[英]Selenium Webdriver - unable to click on button

HTML code: HTML代码:

<div class="buttonBG">  
<input type="button" onclick="window.location.href='?mod=eA&amp;act=00001';"        class="buttonGreen" value="TK">         
<input type="button" onclick="ttoggle('carianDiv');" class="buttonGreen"   value="CK"> 
</div> 

Below is my java code, when I try with below code. 当我尝试以下代码时,以下是我的Java代码。 Can I know whats wrong is in my selenium webdriver code: 我能知道我的Selenium WebDriver代码出什么问题吗:

driver.findElement(By.xpath("//input[@class='buttonGreen' and  contains(@onclick, 'window.location.href='?mod=eA&act=00001')]")).click();

Try to search by value 尝试按价值搜索

driver.findElement(By.cssSelector("[value='TK']")).click();

And for what's wrong, you are searching for ?mod=eA&act=00001 when in the html its 而且出了什么问题,当您在html中搜索它时,您正在搜索?mod=eA&act=00001

?mod=eA&amp;act=00001

Edit 编辑

Another solution is to insert the buttons to list and click by index: 另一种解决方案是插入要列出的按钮并按索引单击:

List<WebElement> buttons = driver.findElements(By.className("buttonGreen"));
buttons.get(0).click();

You can also try using explicit wait 您也可以尝试使用显式等待

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[value='TK']")).click();

This will wait up to 10 seconds for the button to be visible before clicking on it. 单击按钮之前,最多需要等待10秒钟才能看到该按钮。 You can change the selector or time span. 您可以更改选择器或时间跨度。

Try using XPath instead of CSS 尝试使用XPath而不是CSS

driver.find_element(By.XPATH, '//input[@onclick=\'window.location.href=\'?mod=eA&amp;act=00001\';\']').click()

Edit 编辑

Here is the code to switch to iFrame, 这是切换到iFrame的代码,

driver.switchTo().frame("frame_name");

NOTE : After completing the operation inside the iframe, you have to again return back to the main window using the following command. 注意 :在iframe中完成操作后,您必须使用以下命令再次返回主窗口。

driver.switchTo().defaultContent();

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

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