首页 文章

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

提问于
浏览
6

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

这是我一直在尝试的:

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

6 回答

  • 0

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

    open                /page
    click               link=Find us on Facebook!
    pause               1000
    selectWindow
    verifyTextPresent   some text
    
  • 5
    $this->click('css=.sf_admin_action_page:first a');
    
        $this->waitForPopUp('_blank');
        $this->selectWindow('_blank');
    
        $this->waitForElementPresent('css=.t-info:contains(xxx2)');
    

    // ps selenium2

  • 4

    你应该使用 selectPopUp 来聚焦新窗口 . 看到它的文件:

    selectPopUp:

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

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

    • 如果未指定windowID或指定为"null",则会选择第一个非顶部窗口 . 顶部窗口是selectWindow()选择的窗口,不提供windowID . 当多个弹出窗口正在播放时,不应使用此选项 .

    • 否则,将按顺序将windowID视为以下顺序查找窗口:1)窗口的"name",如window.open()所指定; 2)一个javascript变量,它是对窗口的引用; 3)窗口的 Headers . 这与selectWindow执行的顺序查找相同 .

  • 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));
    }
    
  • 1

    只需使用此代码即可 .

    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);
    
  • 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.
    

相关问题