0

How to continue after navigation to each url

I am struck with steps,Grab html content of the page and reg expressions to find the forms

 List<WebElement> demovar=driver.findElements(By.xpath("//a[not(contains(.,'Log Out'))]"));
   System.out.println(demovar.size());

       ArrayList<String> hrefs = new ArrayList<String>(); //List for storing all href values for 'a' tag

          for (WebElement var : demovar) {
              System.out.println(var.getText()); // used to get text present between the anchor tags
              System.out.println(var.getAttribute("href"));
              hrefs.add(var.getAttribute("href")); 
              System.out.println("*************************************");
          }

          //Navigating to each link
          int i=0;
          for (String href : hrefs) {
              driver.navigate().to(href);

              System.out.println((++i)+": navigated to URL with href: "+href);
              Thread.sleep(5000); // To check if the navigation is happening properly.
              System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

              // Grab HTML Content of the page 
           String content = getHtmlContent();


              // Reg.expression finds forms
            //  List<String> listOfForm = getAllPostMethodForms();
sandy
  • 29
  • 6

1 Answers1

1

Using regular expressions for parsing HTML is not the best idea, there are way better locator strategies which can talk to DOM as it's a structured information.

For example you can go for XPath:

  • //form - will return all the <forms> in the page
  • //form[@method='post'] - will return only the ones having POST method

    List<WebElement> forms driver.findElements(By.xpath("//form[@method='post']"));
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133