m_grif
m_grif

Reputation: 79

Using HtmlUnit to click on multiple images on same page that have same source attribute but different onclick events

I'm using HtmlUnit to get data from a webpage. The web page has multiple images that look the same and have the same src value but each one has a different onclick event that directs it to a new page depending on which image was clicked and it is this new page that I need to save the data from. I need to loop through the images and click each one to get the results of the onclick event. At the moment the code I have loops through the images but each file contains the output from the onclick of the first image. Can someone point out where I am missing something please? My code is as follows: customer in the file name is a variable that I have declared earlier in the code and i changes for each loop so that each file has a different name.

DomNodeList<DomElement> iterable2 = page.getElementsByTagName("img");
                       Iterator<DomElement> i3 = iterable2.iterator();
                       int i = 0;
                       while(i3.hasNext())
                       {
                           HtmlElement element1 = null;
                            DomElement anElement = i3.next();
                            if(anElement instanceof HtmlImage)
                            {
                                 HtmlImage input = (HtmlImage) anElement;
                                 if(input.getSrcAttribute().equalsIgnoreCase("customise.gif") )
                                 {
                                      element1 = input;
                                      page2 = element1.click();
                                     webClient.waitForBackgroundJavaScript(30000);
                                     String result = page2.asText();
                                     try {
                                         BufferedWriter out = new BufferedWriter(new FileWriter("Filepath//"+customer+i+".txt"));

                                       out.write(result);
                                       out.close();
                                       } 
                                       catch (IOException e) 
                                       { 
                                       System.out.println("Exception ");

                                       }
                                    i++;
                                 }
                            }

                       }

Upvotes: 0

Views: 1413

Answers (1)

m_grif
m_grif

Reputation: 79

I got this working by using a solution posted for a similar problem on HtmlUnit site which can be found here http://htmlunit.10904.n7.nabble.com/Problem-in-clicking-multiple-javaScript-links-on-a-page-td22682.html

Using the solution in the final posting on the thread in the link my code is now:

                   page = link3.click();
                   // Added the following line of code
                   Object page1Script  = page.getEnclosingWindow().getScriptObject(); 

                   HtmlPage page2 = null;
                   Iterable<DomElement> iterable2 = page.getElementsByTagName("img");
                   Iterator<DomElement> i3 = iterable2.iterator();
                   int i = 0;
                   while(i3.hasNext())
                   {
                       // Added the following two lines of code
                       page.getEnclosingWindow().setEnclosedPage(page); 
                       page.getEnclosingWindow().setScriptObject(page1Script);

                       HtmlElement element1 = null;
                       page2 = null;
                        DomElement anElement = i3.next();
                        if(anElement instanceof HtmlImage)
                        {
                             HtmlImage input = (HtmlImage) anElement;
                             if(input.getSrcAttribute().equalsIgnoreCase("customize.gif") )
                             {
                                 element1 = input;
                                 page2 = element1.click();
                                 webClient.waitForBackgroundJavaScript(30000);
                                 String result = page2.asText();
                                 try {
                                     BufferedWriter out = new BufferedWriter(new FileWriter("Filepath\\"+customer+i+".txt"));


                                   out.write(result);
                                   out.close();
                                   } 
                                   catch (IOException e) 
                                   { 
                                   System.out.println("Exception ");

                                   }
                                i++;
                             }
                        }

                   }

Upvotes: 1

Related Questions