首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java WebDriver等待页面加载

Java WebDriver等待页面加载
EN

Stack Overflow用户
提问于 2012-03-28 16:50:22
回答 2查看 37.9K关注 0票数 8

我想得到页面加载的异常,但仍然没有结果。我使用implicitlyWait设置计时器来抛出异常。

代码语言:javascript
复制
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MILLISECONDS);
driver.get("http://www.rambler.ru");
driver.quit();

有没有人能给我提个建议?我需要这样做,以确保页面加载不会是无限的,如果加载时间将超过我在timer ->中定义的时间,则抛出异常作为结果并跳过TC (作为失败)。

谢谢你,Volodymyr

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-28 17:36:08

为什么在打开页面之前使用隐式等待?尝试使用显式等待。在ramber上找到一些主要的页面元素(例如,搜索文本框)。例如:

代码语言:javascript
复制
 WebDriverWait wait = new WebDriverWait(webDriver, 5);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_search_textbox")));

如果搜索文本框在5秒内没有出现,则until()方法将抛出TimeoutException。

票数 17
EN

Stack Overflow用户

发布于 2013-04-07 03:15:09

我不同意Pavel Zorins answer会工作,因为他没有展示如何处理异常。

下面是我等待iFrame的方法。这要求您的JUnit测试类将RemoteWebDriver的实例传递给页面对象:

代码语言:javascript
复制
public class IFrame1 extends LoadableComponent<IFrame1> {

    private RemoteWebDriver driver;

    @FindBy(id = "iFrame1TextFieldTestInputControlID" )
    public WebElement iFrame1TextFieldInput;

    @FindBy(id = "iFrame1TextFieldTestProcessButtonID" )
    public WebElement copyButton;

    public IFrame1( RemoteWebDriver drv ) {
        super();
        this.driver = drv;
        this.driver.switchTo().defaultContent();
        waitTimer(1, 1000);
        this.driver.switchTo().frame("BodyFrame1");
        LOGGER.info("IFrame1 constructor...");
    }

    @Override
    protected void isLoaded() throws Error {        
        LOGGER.info("IFrame1.isLoaded()...");
        PageFactory.initElements( driver, this );
        try {
            assertTrue( "Page visible title is not yet available.", driver
     .findElementByCssSelector("body form#webDriverUnitiFrame1TestFormID h1")
                    .getText().equals("iFrame1 Test") );
        } catch ( NoSuchElementException e) {
            LOGGER.info("No such element." );
            assertTrue("No such element.", false);
        }
    }

    @Override
    protected void load() {
        LOGGER.info("IFrame1.load()...");
        Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
                .withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring( NoSuchElementException.class ) 
                .ignoring( StaleElementReferenceException.class ) ;
            wait.until( ExpectedConditions.presenceOfElementLocated( 
            By.cssSelector("body form#webDriverUnitiFrame1TestFormID h1") ) );
    }
....

注意:你可以使用see my entire working example here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9904058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档