如何使用selenium验证target =“_ blank”链接?

时间:2010-07-19 14:35:51

标签: selenium

使用target =“_ blank”在新窗口中打开我们页面上的某些链接。如何让selenium看到正确的窗口,以便我可以验证页面是否链接到正确的页面?

以下是我一直在尝试的内容:

open                /page/
click               link=Find us on Facebook!
pause               2000
selectWindow        title=window title
verifyTextPresent   some text

6 个答案:

答案 0 :(得分:5)

您无需将参数传递给selectWindow。浏览器会自动为您的新窗口提供焦点,您只需告诉selenium它已被更改。另外,在确认任何内容之前,请确保为新窗口提供足够的时间进行实际加载:

open                /page
click               link=Find us on Facebook!
pause               1000
selectWindow
verifyTextPresent   some text

答案 1 :(得分:4)

    $this->click('css=.sf_admin_action_page:first a');

    $this->waitForPopUp('_blank');
    $this->selectWindow('_blank');

    $this->waitForElementPresent('css=.t-info:contains(xxx2)');

// ps。 selenium2

答案 2 :(得分:1)

您应该使用selectPopUp来关注新窗口。见其文件:

selectPopUp:

  • 参数:     windowID - 弹出窗口的标识符,可以采用许多不同的含义

  • 简化选择弹出窗口的过程(并且不提供selectWindow()已经提供的功能)。

    • 如果未指定windowID或指定为“null”,则选择第一个非顶部窗口。顶部窗口是selectWindow()选择的窗口,不提供windowID。当多个弹出窗口正在播放时,不应使用此功能。
    • 否则,将按顺序将windowID视为以下顺序查找窗口:1)窗口的“名称”,如window.open()所示; 2)一个javascript变量,它是对窗口的引用; 3)窗口的标题。这与selectWindow执行的顺序查找相同。

答案 3 :(得分:0)

我采取了稍微不同的方法,即强制任何链接使用target = _self,以便可以在同一个窗口中测试它们:

protected void testTextLink(WebDriver driver, final String linkText, final String targetPageTitle, final String targetPagePath) {

    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement link = driver.findElement(By.linkText(linkText));

    // ensure that link always opens in the current window
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].setAttribute('target', arguments[1]);", link, "_self");

    link.click();
    wait.until(ExpectedConditions.titleIs(targetPageTitle));

    // check the target page has the expected title
    assertEquals(driver.getTitle(), targetPageTitle);
    // check the target page has path
    assertTrue(driver.getCurrentUrl().contains(targetPagePath));
}

答案 4 :(得分:0)

只需使用此代码。

public void newtab(){

System.setProperty("webdriver.chrome.driver", "E:\\eclipse\\chromeDriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("http://www.w3schools.com/tags/att_a_target.asp");

//I have provided a sample link. Make sure that you have provided the correct link in the above line.

driver.findElement(By.className("tryitbtn")).click();

new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")), Keys.NUMPAD2).build().perform();


// In keyboard we will press 

//ctrl+1 for 1st tab

//ctrl+2 for 2nd tab

//ctrl+3 for 3rd tab.

//Same action is written in the above code.

}
//Now you can verify the text by using testNG

Assert.assertTrue(condition);

答案 5 :(得分:0)

在本案例中,我们可以使用KeyPress

keyPress(locator,keySequence)     参数:

    locator - an element locator
    keySequence - Either be a string("\" followed by the numeric keycode of the key to be pressed, normally the ASCII value of that key), or a single character. For example: "w", "\119". [Give for CTRL+T]

Simulates a user pressing and releasing a key.