51Testing软件测试论坛

标题: selenium如何取到通过javascript动态加到当前页面的对象 [打印本页]

作者: sandyli119    时间: 2011-5-26 16:50
标题: selenium如何取到通过javascript动态加到当前页面的对象
请问大家:selenium如何取到通过javascript动态加到当前页面的对象。
例如点击某个button会添加一个input框到当前页面,selenium对这个新加进来的input对象捕获不到。
谢谢--
作者: robin.von    时间: 2011-5-26 17:07
selenium本身能支持动态加过来的数据的.

只是有可能数据还没有过来,语句已经执行过去了,所以适当用一些等待的指令就可以了!
作者: sandyli119    时间: 2011-5-27 14:13
回复 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;
                                        }
                                })
作者: robin.von    时间: 2011-5-27 14:26
回复 3# sandyli119


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

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




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2