Saltar al contenido

Cambiar pestañas usando Selenium WebDriver con Java

Verificamos profundamente cada secciones en nuestra página web con el objetivo de enseñarte en todo momento la información con la mayor veracidad y actual.

Solución:

    psdbComponent.clickDocumentLink();
    ArrayList tabs2 = new ArrayList (driver.getWindowHandles());
    driver.switchTo().window(tabs2.get(1));
    driver.close();
    driver.switchTo().window(tabs2.get(0));

Este código funcionó perfectamente para mí. Pruébalo. Siempre debe cambiar su controlador a una nueva pestaña, antes de querer hacer algo en una nueva pestaña.

Esta es una solución simple para abrir una nueva pestaña, cambiar el enfoque, cerrar la pestaña y regresar el enfoque a la pestaña anterior/original:

@Test
public void testTabs() 
    driver.get("https://business.twitter.com/start-advertising");
    assertStartAdvertising();

    // considering that there is only one tab opened in that point.
    String oldTab = driver.getWindowHandle();
    driver.findElement(By.linkText("Twitter Advertising Blog")).click();
    ArrayList newTab = new ArrayList(driver.getWindowHandles());
    newTab.remove(oldTab);
    // change focus to new tab
    driver.switchTo().window(newTab.get(0));
    assertAdvertisingBlog();

    // Do what you want here, you are in the new tab

    driver.close();
    // change focus back to old tab
    driver.switchTo().window(oldTab);
    assertStartAdvertising();

    // Do what you want here, you are in the old tab


private void assertStartAdvertising()  Twitter for Business", driver.getTitle());


private void assertAdvertisingBlog() 
    assertEquals("Twitter Advertising", driver.getTitle());

Hay una diferencia en cómo el controlador web maneja diferentes ventanas y cómo maneja diferentes pestañas.

Caso 1:

En caso de que haya varias ventanas, el siguiente código puede ayudar:

//Get the current window handle
String windowHandle = driver.getWindowHandle();

//Get the list of window handles
ArrayList tabs = new ArrayList (driver.getWindowHandles());
System.out.println(tabs.size());
//Use the list of window handles to switch between windows
driver.switchTo().window(tabs.get(0));

//Switch back to original window
driver.switchTo().window(mainWindowHandle);

Caso 2:

En caso de que haya varias pestañas en la misma ventana, solo hay un identificador de ventana. Por lo tanto, cambiar entre los identificadores de ventana mantiene el control en la misma pestaña.
En este caso, usar Ctrl + t (Ctrl + Tabulador) para cambiar entre pestañas es más útil.

//Open a new tab using Ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Switch between tabs using Ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

El código de ejemplo detallado se puede encontrar aquí:
http://design-interviews.blogspot.com/2014/11/switching- between-tabs-in-same-browser-window.html

Si te gusta la informática, tienes la libertad de dejar un artículo acerca de qué le añadirías a esta crónica.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *