selenium动态抓取页面元素

来源:互联网 发布:安卓编程颜色代码 编辑:程序博客网 时间:2024/05/10 15:36

在测试拍客的时候发现,有时候需要删除,转发拍客,a标签属性id,name都是动态变化的,和发的拍客id有关系,这样测试的时候,就不太方便,总是要去变化id,不然就会报:Element is not found了。

 

今天找到了一种方法,详见下面详细的描述哦

用selenium录制的脚本如下:

 

@Test

public void testDiv() throws Exception {

selenium.open("/webroot/apps/paike/index.php?s=/Index/index");

this.login();

Thread.sleep(10000);

selenium.click("recomment516");

}

 

改良后的代码如下:

 

@Test

public void testDiv() throws Exception {

selenium.open("/webroot/apps/paike/index.php?s=/Index/index");

this.login();

Thread.sleep(10000);

selenium.click("//div[@class='Ainfo']/div[@class='Fhandle']/a[3]");

}

 

在第二个div下有3个a标签,选择第三个,所以用a[3],第二个就是a[2],以此类推,其他的标签未试,应该也是如此。

 

 

原创粉丝点击