Webdriver target = "_ blank"

The page has an image with a hyperlink and that the hyperlink has target = "_ blank", and every time I click this image, a new firefox loads and the hyperlink redirects to this new firefox web page and I lose control of this web page. Is it possible to delete or change this target = "_ blank" on the hyperlink, bcause I want to load the web page into the same webdriver

WebDriver driver = new FirefoxDriver(); driver.get("http://www.page.eu/"); WebElement submit; submit = driver.findElement(By.xpath("//img[@alt='page']")); submit.click(); 

that the hyperlink has target = "_ blank" Do I need to somehow change this target using perhaps webdriver + javascript or what? is it possible?

edited

thanks for the suggestions, but still I tried to make this problem as Grooveek , but no changes

 WebElement labels2 = driver.findElement(By.xpath("//a[@href='http://tahtpage.net']")); WebElement aa = (WebElement) ((JavascriptExecutor) driver).executeScript("labels2.setAttribute('target','_self')",labels2 ); aa.click(); 

I have an error org.openqa.selenium.WebDriverException: null (WARNING: the server did not provide any stack information)

I am not good at javascrit, so I think the problem is that the executor

+4
source share
6 answers

Try the following:

 WebElement labels2 = driver.findElement(By.xpath("//a[@href='http://tahtpage.net']")); WebElement aa = (WebElement) ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('target','_self')",labels2 ); aa.click(); 

You get an empty exception because you use labels2 in your javascript, which does not exist in this context. Changing it to arguments[0] , Selenium will accept the label2 parameter and refer to it accordingly in javascript.

+1
source

Instead of clicking on the image, you can just go to the URL in the link:

 WebElement link = (driver.findElement(By.xpath("//img[@alt='page']/parent::*")); String href = link.getAttribute("href"); driver.get(href); 
+2
source

Evaluating javascript in a window will help you suppress target = blank links Here is an example from Webdriver Docs

 List<WebElement> labels = driver.findElements(By.tagName("label")); List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript( "var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" + "inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels); 

Adapt it to change the DOM to reset target = "_ blank links"

+1
source

Why don't you use SwitchTo (). Window?

+1
source

I think you should use SwitchTo (). The window proposed by Simeon Sinichkin. however, I did not like his example. Here is a simple example.

  driver.Navigate().GoToUrl(baseURL); //Get the Main Window Handle var baseWindow = driver.CurrentWindowHandle; // navigate to another window (_blank) driver.FindElement(By.Id("btn_help")).Click(); Thread.Sleep(2000); //switch back to Main Window driver.SwitchTo().Window(baseWindow); 

hope that helps

+1
source

Why don't you use:

 target="_self" 
0
source

Source: https://habr.com/ru/post/1390828/


All Articles