首页 > 解决方案 > selenium 单击指向新选项卡的链接后,他看不到按钮和字段

问题描述

我的问题如下:我点击在浏览器中打开一个新标签的链接,我切换到这个标签,我想点击该字段来输入文本。不幸的是,它返回给我:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='nameFields']"}

我尝试了各种方法来关闭第一个选项卡(tabs.get(0))。

    public exampleMethod() {
        ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
        driver.switchTo().window(tabs.get(0));
        System.out.println("tab 0: " + driver.getTitle());
        driver.close();
        driver.switchTo().window(tabs.get(1));
        System.out.println("tab 1: " + driver.getTitle());
        //driver.navigate().refresh();
        //driver.findElement(By.xpath("//*[@id='nameInput']")).sendKeys("qwerty");
        nameInput.sendKeys("qwerty");
        submit.click();
        return this;
    }
There is no problem with listing the page titles.

标签: javaseleniumtabsnew-operator

解决方案


Bbefore nameInput.sendKeys("qwerty"),通过下面的代码使用wait

WebElement we = new WebDriverWait(driver, 25).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='nameInput']")));

推荐阅读