Как получить Alert Box в chrome с помощью Selenium - PullRequest
0 голосов
/ 16 января 2020
package com.company;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.json.JsonOutput;

import java.io.IOException;
import java.security.Key;
import java.sql.SQLOutput;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

        public class Main {

        public static void main(String[] args) throws IOException, InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Logan\\Downloads\\selenium-java-3.141.59\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        Alert alert = driver.switchTo().alert();
        String msg = alert.getText();

Я хочу получить окно оповещения при открытии окна. Я пытался отложить процесс с помощью Thread.sleep, но он все еще не работает.

Ответы [ 2 ]

0 голосов
/ 16 января 2020

Вы можете использовать явное ожидание, пока предупреждение не появится, прежде чем переключиться на него. Попробуйте ниже код

new WebDriverWait(driver, 40)
        .ignoring(NoAlertPresentException.class)
        .until(ExpectedConditions.alertIsPresent());

Alert al = driver.switchTo().alert();

String msg = al.getText();
0 голосов
/ 16 января 2020

Вместо thread.sleep() вы можете вызвать WebDriverWait для нового ExpectedConditions.alertIsPresent(), чтобы убедиться, что предупреждение полностью загружено, прежде чем пытаться вызвать alert.getText():

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
// add the above import statements

// wait for alert to exist
new WebDriverWait(driver, 30).until(ExpectedConditions.alertIsPresent());

Alert alert = driver.switchTo().alert();
String msg = alert.getText();

Если это не работает для Вы, ваш браузер, возможно, не отображаете истинное предупреждение - вы можете попытаться проверить всплывающее окно и посмотреть, есть ли за ним элементы HTML, чтобы проверить это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...