[英]In selenium, Not able to click on Error msg 'OK' button
I'm trying to click on OK Button using selenium, cant find the elememt. 我正在尝试使用硒单击“确定”按钮,找不到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
). text()
仅选择当前上下文节点( button
)下的文本子节点。 There is no text node that contains OK in button
. 在
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完成:
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 @Rupesh和@Artjom B.谢谢
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.