Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
How to click on a link which should be open in same window using Selenium Java

I'm just self studying and trying to Automate the Flipkart site using Selenium- Java.

So here's the scenario: I want to click on a product link whose attribute is set as Target=_blank.

I want to set it as _self.

I have written one code using JS Executor but getting error in Runtime :

WebElement linkpath = driver.findElement(By.xpath("//div[text()='vivo Y12G (Glacier Blue, 64 GB)']"));

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("document.getElementByXpath("+linkpath+").setAttribute('target', 'self')");

linkpath.click();

Error:

org.openqa.selenium.JavascriptException: javascript error: Unexpected token ':'

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue with the code is with this line:

js.executeScript("document.getElementByXpath("+linkpath+").setAttribute('target', 'self')");

You are attempting to pass an object (the element identified by your first line of code) instead of an xpath string (getElementByXpath)

You have 2 options:

  1. Declare xpath as a String at the beginning so it can be used for both lines of code that require it
String xpath = "//div[text()='vivo Y12G (Glacier Blue, 64 GB)']"
WebElement linkpath = driver.findElement(By.xpath(xpath));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementByXpath(xpath).setAttribute('target', 'self')");
linkpath.click();
  1. Amend the JavascriptExecutor to use the Webelement object already found as an argument
WebElement linkpath = driver.findElement(By.xpath("//div[text()='vivo Y12G (Glacier Blue, 64 GB)']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('target', 'self')",linkpath);
linkpath.click();
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda