简体   繁体   中英

Selenium Webdriver - unable to click on button

HTML code:

<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. Can I know whats wrong is in my selenium webdriver code:

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

?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. You can change the selector or time span.

Try using XPath instead of 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,

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.

driver.switchTo().defaultContent();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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