Robert Barfield
Robert Barfield

Reputation: 21

Selenium, open new window

I'm trying to use selenium and java to click either on the link or button (shown in html below) and assert that the number of tabs or windows increased, and then close only the new tab/window.

<div id="req7">
<h2>Test #7</h2>
<button onclick="window.open('');" name="button">Open New Window</button>
<br>
<a target="_blank" href="about:blank" name="newTab">Open New Tab</a>
</div>

How would I go about this in Java? Thanks!

Upvotes: 0

Views: 891

Answers (1)

Ya Wang
Ya Wang

Reputation: 1808

Clicking on a button Selenium

// driver can be chrome or something 
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.name("button"))); 
myDynamicElement.click();

If you want to check if you have a new tab or window opened you should get the length of the handles.

//---- before click -----
int initalHandleCount = driver.getWindowHandles().size();

// ---- after click ----- 
boolean hasNewPage = initalHandleCount < driver.getWindowHandles().size(); 

Upvotes: 0

Related Questions