访问子页面中的id

时间:2014-07-16 09:56:58

标签: selenium junit selenium-webdriver junit4

我想用Selenium WebDriver测试我的网页。我的网页包含两个子页面,如下面的代码所示:

globalPage.html

<body>
    <div class="container">
        <section class="tabs">
            <input id="tab-1" type="radio" name="radio-set" class="tab-selector-1" checked="checked" />
            <label for="tab-1" class="tab-label-1">tab1</label> 
            <input id="tab-2" type="radio" name="radio-set" class="tab-selector-2" />
            <label for="tab-2" class="tab-label-2">tab2</label>     
            <div class="clear-shadow"></div>            

            <div class="content">
                <div class="content-1">                       
                    <object id="formM" type="text/html" data="page1.html" width="848" height="720"></object>
                </div>
                <div class="content-2">
                    <object id="commT" type="text/html" data="page2.html" width="960" height="630"></object>
                </div>
            </div>

        </section>
    </div>      
</body>

page1.html包含一个id为&#34; myId1&#34;的元素。

page1.html

...
...
<div class="preview">
    <fieldset id="myId1">
        <form name="page">
            <label for="cb" id="labelPop">pop:</label> 
                <input type="checkbox" id="cb" name="cb" checked="checked" onclick="valid();showPop();" /><br />            
            <label for="ta" id="labelMessage2">Message:</label> 
                <textarea disabled="true" id="ta" name="ta" cols="22" rows="9"></textarea>          
            <label id="labelFile2" >file:</label>
            <input type="file" name="validate" id="upload2" disabled="disabled"/>                                               
        </form>     
    </fieldset>

    <fieldset id="myId2">                   
        <label id="labelPrio">Prio:</label>
            <select id="selectPrio">
                <option value="one">High</option>
                <option value="two">Low</option>
            </select><p></p>
        <label id="labelMessage1">Message:</label>
            <textarea  id="areaa" cols="17" rows="8"></textarea><p></p>                                                             
        <form enctype="multipart/form-data" method="post">
            <label id="labelFile1">file:</label>
            <input id="upload1" type="file" name="file[]" >
        </form>                             
    </fieldset> 
</div>
...
...

我的代码Selenium是:

....
....
@Before
    public void setUp() throws Exception {
        final Properties properties = System.getProperties();
        this.baseUrl = properties.getProperty("base.url", "http://monIp:8080/TestPage");
    }

    @Test
    public void firefoxTest() throws Exception {
        this.driver = new FirefoxDriver();
        testSelenium();
    }

    private void testSelenium() throws Exception {
        this.driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        this.driver.get(this.baseUrl + "faces/globalPage.html");

        new Select(this.driver.findElement(By.xpath("//fieldset[@id='myId1']/select[3]")))
    }
    ....
    ....

我有一个错误:无法找到元素:{"method":"xpath","selector":"//fieldset[@id='myId1']/select[3]"} 我该如何解决这个问题?

当我尝试

this.driver.get(this.driver.get(this.baseUrl + "faces/page1.html"); 

效果很好,但我没有标签。

所以我的问题是:我怎样才能访问&#34; id&#34;在子页面?

感谢您的帮助!

0 个答案:

没有答案