IE 9的Selenium Webdriver - 没有这样的元素

时间:2014-12-03 18:46:44

标签: java selenium selenium-webdriver

我自动化外部产品,我无法访问源代码。

这是视图源代码

<div class="row-fluid">
        <div class="span10 offset1" id="home">
                <div id="myCarousel" class="carousel slide">
                    <div id="myCarousel-heading">
                        <p> What's new </p>
                </div>
                <div class="carousel-inner">
                        <div class="item active">
                            <a href="CreateUserDocument.aspx?code=Brochure_Print_Ship_Advice">
                                <img src="Custom/Themes/Vanguard/Inserts/images/carousel/Crsl_Advice.jpg" alt="">
                        </a>
                    </div><!-- End of div.item -->

                     <div class="item">
                            <a href="CreateUserDocument.aspx?code=Invite_Print_Mail_RES_Mtg">
                                <img src="Custom/Themes/Vanguard/Inserts/images/carousel/Crsl_banner_all_green.jpg" alt="">
                        </a>
                      </div><!-- End of div.item -->

                      <div class="item">
                        <a href="CreateUserDocument.aspx?code=Presentation_PDF_RES_Mtg">
                            <img src="Custom/Themes/Vanguard/Inserts/images/carousel/Crsl_RES_Pres.jpg" alt="">
                        </a>
                      </div><!-- End of div.item -->
                        </div>

我想在页面加载后单击最后一个超链接.Landing页面有sso身份验证,所以我在页面加载后添加了隐式等待20秒。

java代码

driver = (WebDriver) new InternetExplorerDriver(capab1);
driver.get("url");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[@href = 'CreateUserDocument.aspx?code=Presentation_PDF_RES_Mtg']")).click();

我在32位IE 9浏览器中使用Selenium 2.44库和2.32 IEDriverServer.exe。

感谢任何想法。谢谢!

1 个答案:

答案 0 :(得分:0)

从我所看到的,它是 Carousel Slider 。所以,可能你必须等到物品出现。请尝试以下代码,看看它是否有效:

    try{
        WebDriverWait wait = new WebDriverWait(driver,40);
        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href = 'CreateUserDocument.aspx?code=Presentation_PDF_RES_Mtg']")));
        element.click();
    }catch(Throwable e){
        System.err.println("Error found while waiting for the element and clicking it: "+e.getMessage());
    }

在使用以下代码切换到iframe之前:

 try{
     WebDriverWait wait = new WebDriverWait(driver,30);
     wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("widgetFrame"));
    }catch(Throwable e){
        System.err.println("Error came while waiting and switching to frame. "+e.getMessage());
    }