selenium对象标识在同一窗口中从一个页面导航到另一个页面时出现问题

时间:2015-05-06 07:15:31

标签: selenium selenium-webdriver

在同一窗口中从一个网页导航到另一个网页时,selenium显示在第二页上没有找到这样的元素。例如,它能够在成功登录后找到登录页面之前的对象,而不是识别对象。请告诉我为什么会这样,以及它的解决方案是什么。对于第二种方法,我面临的问题。我用过线程。睡眠和切换窗口仍然面临问题。

public void LoginKaplan() {

        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        kecUserNameTextBox.sendKeys("abc");
        kecPasswordTextBox.sendKeys("123#");
        userNameTextBox.sendKeys("a1");
        passwordTextBox.sendKeys("1234");

    driver.findElement(By.xpath("//div[@id='productName_chosen']/a/span"))
                .click();
        driver.findElement(
                By.xpath("//div[@id='productName_chosen']/div/div/input"))
                .sendKeys("DAT2015");

        WebDriverWait wait = new WebDriverWait(driver, 120);

        List<WebElement> findElements = driver.findElements(By
                .xpath("//*[@id='productName_chosen']/div/ul/li[2]/em"));
        WebElement element = wait
                .until(ExpectedConditions.elementToBeClickable(By
                        .xpath("//*[@id='productName_chosen']/div/ul/li[2]/em")));

        for (WebElement webElement : findElements) {
            if (webElement.getText().equals("DAT2015")) {
                driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                webElement.click();
                break;
            }
        }
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        driver.findElement(By.xpath("//div[@id='syllabusPath_chosen']/a/span"))
                .click();
        driver.findElement(
                By.xpath("//div[@id='syllabusPath_chosen']/div/div/input"))
                .click();

        driver.findElement(
                By.xpath("//div[@id='syllabusPath_chosen']/div/div/input"))
                .sendKeys("/DAT2015/assessments/secTests/qdatoatnatsciA15");
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        List<WebElement> findElements2 = driver.findElements(By
                .xpath("//div[@id='syllabusPath_chosen']/div/ul/li/em"));
        WebElement element2 = wait
                .until(ExpectedConditions.elementToBeClickable(By
                        .xpath("//div[@id='syllabusPath_chosen']/div/ul/li/em")));
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        for (WebElement webElement : findElements2) {
            if (webElement.getText().equals(
                    "/DAT2015/assessments/secTests/qdatoatnatsciA15")) {
                driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                webElement.click();
                break;
            }
        }

        loginButton.click();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        driver.switchTo().defaultContent();
}

//Method 2----------------------------------------------------
    public void QuestionsAns() {
        try {
            Thread.sleep(22000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        driver.findElement(
                By.id("subitem0.singleAnswerMultipleChoice.answerChoiceRow1.state0"))
                .click();
        driver.findElement(By.id("img2.simNext")).click();
        driver.findElement(
                By.id("subitem0.singleAnswerMultipleChoice.answerChoiceRow2.state0"))
                .click();
        driver.findElement(By.id("img2.simNext")).click();
        driver.findElement(
                By.id("subitem0.singleAnswerMultipleChoice.answerChoiceRow2.state0"))
                .click();
        driver.findElement(By.id("img2.simNext")).click();

    }

2 个答案:

答案 0 :(得分:0)

等待对象出现

WebDriverWait wait;
wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by)));
wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by)));

这些等待函数将一直等到对象在网页上可点击或可见。

答案 1 :(得分:0)

查看来源并搜索“iframe”标记。您将获得标签名称,您也可以在帧之间切换。你可以使用driver.switchTo().defaultContent();

driver.switch_to.frame(0); //try with index 0,1