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
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With