Getting inner content of dynamically generated <div> in WebDriver

820 views
Skip to first unread message

宁 邹

unread,
Oct 20, 2011, 3:25:29 AM10/20/11
to Selenium Users
Hi all,

I met an issue of find an element which generated dynamically in a
<div> and this part is content of ajax.. Since our portal use
javascript in the webpage, I would like to use WebDriverWait to judge
if the element loaded. It works fine for those static element. When my
program try to find element which generated dynamically, it always
reports timeout...

Here is part of web page source code:
Tab link:
<li id="accessctl_newtab"><a href="/portal/configure/
policy_accessctl.mhtml?sid=.......">Access Control</a></li>
it's used for generate the following content:
<div id="ajaxContent">(xxxxxxxxxxxx generated dynamically
xxxxxxxxxxxxxxxxxxxx)</div>

I implement interface ExpectedCondition<Boolean>, like this,
public class ExpectedName implements ExpectedCondition<Boolean> {

public String elementName;

public ExpectedName(String elementName){

this.elementName=elementName;
}
/* (non-Javadoc)
* @see com.google.common.base.Function#apply(java.lang.Object)
*/
@Override
public Boolean apply(WebDriver arg0) {
// TODO Auto-generated method stub
return arg0.findElement(By.name(elementName)) != null;
}

}

So outside, i would like to generate a object for it and use
WebDriverWait to see if this element loaded in page:
ExpectedLinkText ei=new ExpectedLinkText(elementName);
(new WebDriverWait(driver, timeOutInSeconds)).until(ei);

But this does not work at all. it always time out...Seems the driver
could not find the element...
Well, the weird thing is, if I use thread.sleep(x) wait for several
seconds, then I use findElement driectly, it works!
Thread.sleep(3000);
WebElement element=driver.findElement(By.name("xxxxxx"));
element.click();

Anyone has the similar issue? I don't want to use sleep...Any method
could resolve this issue?

Many thanks!

Josh

unread,
Oct 20, 2011, 5:10:34 PM10/20/11
to seleniu...@googlegroups.com
Make sure that your div isn't getting generated inside of an iframe. If it is, you will need to navigate to the appropriate iframe first. Also, the way you are implementing your WebDriverWait seems to be incorrect. There is a reference on the selenium page here: http://seleniumhq.org/docs/04_webdriver_advanced.html that can show you the proper syntax of the WebDriverWait.Until() method.

Try declaring the method you need to pass into the Until method inline like this:

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(new ExpectedCondition<WebElement>(){
    @Override
    public WebElement apply(WebDriver d) {
        return d.findElement(By.name("xxxxx"));
    }});

宁 邹

unread,
Oct 21, 2011, 12:15:55 AM10/21/11
to Selenium Users
Hi Josh,

Thanks for your reply. I had a try, if I use inner class to do that,
it could find the element that I want to look for!

But if I implement the ExpectedContidition<T> interface with a class,
it will not find it.

I just try to figure out a way so that I don't need to write code to
implement interface ExpectedCondition<T> and its apply() method every
time.

Any inputs?

Thanks a lot!

On Oct 21, 5:10 am, Josh <jteit...@gmail.com> wrote:
> Make sure that your div isn't getting generated inside of an iframe. If it
> is, you will need to navigate to the appropriate iframe first. Also, the way
> you are implementing your WebDriverWait seems to be incorrect. There is a
> reference on the selenium page here:http://seleniumhq.org/docs/04_webdriver_advanced.htmlthat can show you the

Krishnan Mahadevan

unread,
Oct 21, 2011, 1:26:21 AM10/21/11
to seleniu...@googlegroups.com
Rather than instantianting the interface on the fly and implementing the apply() method everytime, I guess you are better off in having a new class implement this interface, define argumented constructors (or) getters/setters as deemed necessary and just stick to instantiating your custom class and working with it.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


宁 邹

unread,
Oct 21, 2011, 2:40:26 AM10/21/11
to Selenium Users
Hi all,

Thanks for you quick responses and help!

I've done it. :)

Create a class and write a method for it like,

public WebElement wait4IdPresent(WebDriver driver,final String
elementId, int timeOutInSeconds){

WebElement we=null;
try{
WebDriverWait wdw=new WebDriverWait(driver, timeOutInSeconds);

if((we=wdw.until(new ExpectedCondition<WebElement>(){
/* (non-Javadoc)
* @see com.google.common.base.Function#apply(java.lang.Object)
*/
@Override
public WebElement apply(WebDriver d) {
// TODO Auto-generated method stub
return d.findElement(By.id(elementId));
}
}))!=null){
report.report("Element "+elementId+" found!");
}
}catch(Exception e){
report.report("Element "+elementId+" was not found. Time out.",
false);
}
return we;

}

Thanks all again!

On Oct 21, 1:26 pm, Krishnan Mahadevan
> >http://seleniumhq.org/docs/04_webdriver_advanced.htmlthatcan show you the
Reply all
Reply to author
Forward
0 new messages