I'm trying to click on OK Button using selenium, cant find the elememt.
objBrowser.findElement(By.xpath("//button[contains(text(), 'OK')]")).click();
OK button Inspect element code as below
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false" jQuery15109548211953493255="16">
<span class="ui-button-text"> Text - OK
text()
only selects text child nodes under the current context node ( button
). There is no text node that contains OK in button
.
You need most likely:
By.xpath("//button[contains(span/text(), 'OK')]")
If the element is not immediately visible or clickable, you need to wait for it. This is usually done with a WebdriverWait:
WebDriverWait wait = new WebDriverWait(webDriver, 3); // 3 seconds at most
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(...));
尝试使用cssSelector。
objBrowser.findElement(By.cssSelector("button:contains('Ok')")).click();
Include it in your test code
import org.openqa.selenium.Alert;
and after the action which opens the alert message
Try below code
Alert alert_test = driver.switchTo().alert();
alert_test.accept();
Let me know the error message in-case if it does not works.
For Modal pop-up window , Try below approach and verify.
Use below code after the action which opens the alert message
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
Note: Some-times it takes few seconds to recognize the modal pop-up window present but some times it goes a bit long,so below time you can modify as per your need.
Thread.sleep(7000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
@Rupesh and @Artjom B. Thanks
I checked and both of your answers are working.
But the real problem was at the button where error msg pops up. so i added sleep before remove button on which popup occurs and this is working with both of your answers.
Thread.sleep(1000);
objBrowser.findElement(By.cssSelector("input[Value='Remove']")).click();
objBrowser.findElement(By.xpath("//button[contains(span/text(), 'OK')]")).click();
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.