Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Cómo hacer clic en un enlace que debería estar abierto en la misma ventana usando Selenium Java

Solo estoy estudiando por mi cuenta y tratando de automatizar el sitio Flipkart usando Selenium-Java.

Así que este es el escenario: quiero hacer clic en un enlace de producto cuyo atributo se establece como Target=_blank.

Quiero configurarlo como _self.

Escribí un código usando JS Executor pero obtuve un error en 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: error de javascript: Token inesperado ':'

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El problema con el código es con esta línea:

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

Está intentando pasar un objeto (el elemento identificado por su primera línea de código) en lugar de una cadena xpath (getElementByXpath)

Tienes 2 opciones:

  1. Declare xpath como una cadena al principio para que pueda usarse para las dos líneas de código que lo requieren
 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. Modifique JavascriptExecutor para usar el objeto Webelement ya encontrado como argumento
 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!