51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 4241|回复: 3
打印 上一主题 下一主题

selenium如何取到通过javascript动态加到当前页面的对象

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-5-26 16:50:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
请问大家:selenium如何取到通过javascript动态加到当前页面的对象。
例如点击某个button会添加一个input框到当前页面,selenium对这个新加进来的input对象捕获不到。
谢谢--
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2011-5-26 17:07:44 | 只看该作者
selenium本身能支持动态加过来的数据的.

只是有可能数据还没有过来,语句已经执行过去了,所以适当用一些等待的指令就可以了!
回复 支持 反对

使用道具 举报

该用户从未签到

3#
 楼主| 发表于 2011-5-27 14:13:39 | 只看该作者
回复 2# robin.von

搜索了selenium2 的 FAQ有提到过
Q: WebDriver fails to find elements / Does not block on page loads

This problem can manifest itself in various ways:

    * Using WebDriver.findElement(...) throws ElementNotFoundException, but the element is clearly there - inspecting the DOM (using Firebug, etc) clearly shows it.
    * Calling Driver.get returns once the HTML has been loaded - but Javascript code triggered by the onload event was not done, so the page is incomplete and some elements cannot be found.
    * Clicking on an element / link triggers an operation that creates new element. However, calling findElement(s) after click returns does not find it. Isn't click supposed to be blocking?
    * How do I know when a page has finished loading?

Explanation: WebDriver has a blocking API, generally. However, under some conditions it is possible for a get call to return before the page has finished loading. The classic example is Javascript starting to run after the page has loaded (triggered by onload). Browsers (e.g. Firefox) will notify WebDriver when the basic HTML content has been loaded, which is when WebDriver returns. It's difficult (if not impossible) to know when Javascript has finished executing, since JS code may schedule functions to be called in the future, depend on server response, etc. This is also true for clicking - when the platform supports native events (Windows, Linux) clicking is done by sending a mouse click event with the element's coordinates at the OS level - WebDriver cannot track the exact sequence of operations this click creates. For this reason, the blocking API is imperfect - WebDriver cannot wait for all conditions to be met before the test proceeds because it does not know them. Usually, the important matter is whether the element involved in the next interaction is present and ready.

Solution: Use the Wait class to wait for a specific element to appear. This class simply calls findElement over and over, discarding the NoSuchElementException each time, until the element is found (or a timeout has expired). Since this is the behaviour desired by default for many users, a mechanism for implicitly-waiting for elements to appear has been implemented. This is accessible through the WebDriver.manage().timeouts() call. (This was previously tracked on issue 26).

但是我用了wait类去等待对象加载进来,还是取不到直到超时。
wait.until(new ExpectedCondition<Boolean>() {
                                public Boolean apply(WebDriver webDriver) {
                                        System.out.println("Searching ...");
                                        return webDriver.findElement(By.id("itemType1")) != null;
                                        }
                                })
回复 支持 反对

使用道具 举报

该用户从未签到

4#
发表于 2011-5-27 14:26:06 | 只看该作者
回复 3# sandyli119


    上面段话也是说selenium是支持JS动态加进来的元素的,所需要做的只是在自己写的代码中要等待这个元素在页面上加载完成.

至于具体的方法中为什么不能抓到,还是多调试几次吧,
可以试试把超时时间设得很长,然后停下来用FireBug查看页面的源代码,看所需要的元素是否载入成功,元素属性以及在DOM树中位置是否和期望一致.再尝试用XPATH能否在页面上正确定位到元素.不用WEBDriver,用selenium IDE看是否能抓到这个元素
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-22 18:15 , Processed in 0.070503 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表