英文:
I want to catch a popup message and get its text content
问题
如果至少有一个字段为空,则当您点击“Login”按钮时,我们会收到一个弹出消息,上面写着“Please fill out this field”。我需要检查是否出现了这个消息。但我无法以通常的方式捕获它。
WebDriver driver = new ChromeDriver();
driver.get("https://gdcloud.ru/release-17");
driver.findElement(By.id("login_button")).click();
Alert alert = driver.switchTo().alert();
String alertMessage = alert.getText();
System.out.println("message: " + alertMessage);
我做错了什么?
英文:
If at least one field is empty, then when you click on the "Login" button, we receive a popup message "Please fill out this field". I need to check if this message appears. But I cannot capture it in the usual way.
WebDriver driver = new ChromeDriver();
driver.get("https://gdcloud.ru/release-17");
driver.findElement(By.id("login_button")).click();
Alert alert = driver.switchTo().alert();
String alertMessage = alert.getText();
System.out.println("message: " + alertMessage);
What am I doing wrong?
答案1
得分: 0
这不是你可以使用 driver.switchTo().alert();
处理的常规警告框。它只是在将 "required" 作为文本字段的属性时触发的HTML5表单验证,因此你无法使用常规的Selenium代码验证它。但是有一个解决方法。你可以使用Sikuli/AutoIt来检查提示消息的存在,但请记住,不同的浏览器提示看起来不同。因此,要么只使用一个浏览器,要么根据你正在使用的驱动程序检查屏幕上图像(提示)的存在,然后使用Sikuli/AutoIt。
英文:
That is not the regular alert box that you could handle with driver.switchTo().alert();
. It is just the HTML5 form validation which comes into action when you put "required" as the attribute of textfield and hence you cannot validate this with the help regular selenium code. But there is a workaround. Either you can use sikuli/autoit to check the presence of that tooltip message but remember, for different browsers, the tooltip look different. So either stick to one browser only or check for the driver that you are using and based on that check for that presence of image(tooltip) on the screen with sikuli/autoIt.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论