Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webdriver target="_blank"

Page has image with hyperlink and that hyperlink has target="_blank" and every time i press that image loads new firefox and that hyperlink is redirected to that new firefox web and i lose all control of that webpage. Is possilble to remove or change that target="_blank" on hyperlink, bcause i want to load webpage in 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 hyperlink have target="_blank" i need to change that target somehow by using webdriver + javascript maybe or what? is it possible?

edited

thanks for suggestions, but still is this problem i tried to make like Grooveek said 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 stacktrace information)

i'm not good at javascrit so i think is problem in that executor

like image 400
Palaima Avatar asked Jan 13 '12 12:01

Palaima


2 Answers

Instead of clicking on the image, you could just directly 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);
like image 151
Jeremiah Orr Avatar answered Sep 22 '22 14:09

Jeremiah Orr


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 are getting a null exception because you are using labels2 in your javascript, which doesn't exist in that context. By changing it to arguments[0] Selenium will take the labels2 parameter and reference it in javascript appropriately.

like image 23
prestomanifesto Avatar answered Sep 23 '22 14:09

prestomanifesto