Drive Alert using Selenium in Java

Asked

Viewed 711 times

2

In addition to handling javascript alerts with Selenium Webdriver, it would be possible to invoke Selenium code execution-time alerts in the browser?

I would like to create the 3 types of javascript alert below using Selenium:

  • alerta
  • confirmação
  • prompt de entrada.

To facilitate understanding...

When accessing any page (I used w3c as an example) with Selenium:

WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/JS/");

I want to invoke the 3 types of alert below with dynamic values.

inserir a descrição da imagem aqui

3 answers

3


I believe with Javascript to do, something like:

driver.executeScript("window.alert('teste')")
String confirmation = driver.executeScript("return window.confirm('confirmação')");
String result = driver.executeScript("return prompt('Por favor, insira seu nome:', 'Harry Potter')");

Source: https://www.w3schools.com/js/js_popup.asp

  • Ball show man, that was it!

2

Something nice to leave noted in case someone looks for it too. I was wondering how to go through Alert. I found the following script that worked:

    // pega o alert que está aberto
    Alert alert = driver.switchTo().alert();
    // confirma
    alert.accept();

It worked! I found the script for an Alura course referring to Lenium.

0

to interact with the Alerts I do the following

Just like @João

// pega o alert que está aberto
    Alert alert = driver.switchTo().alert();
    // confirma
    alert.accept();

But I also add:

    // aperta o cancelar
      alert.dismiss();
   // escreve no prompt
      alert.sendKeys("Escrevi no prompt");

Reference:

Documentation Selenium

Browser other questions tagged

You are not signed in. Login or sign up in order to post.