-1

I want to select an element with specific text from the HTML using JSoup. The HTML is

        <div class="logonContainer" style="padding-left: 300px; background-color: #3d5fa3; padding-top: 110px"> 
         <div id="bglogodiv" style="background-image: url(15.0.847/themes/resources/ctslogo.jpg); border-radius: 25px; width: 800px"> 
          <br /> 
          <br /> 
          <div id="lgnDiv" class="logonDiv" onkeypress="return checkSubmit(event)"> 
           <div class="signInImageHeader" role="heading" aria-label="Outlook Web App "> 
            <img class="mouseHeader" src="/owa/auth/15.0.1263/themes/resources/owa_text_blue.png" alt="Outlook Web App " /> 
           </div> 
           <div> 
            <div id="right" style="width: auto; float: right"> 
             <div class="signInInputLabel" id="userNameLabel" aria-hidden="true">
              User name:
             </div> 
             <div> 
              <input id="username" name="username" class="signInInputText" role="textbox" aria-labelledby="userNameLabel" /> 
             </div> 
             <div class="signInInputLabel" id="passwordLabel" aria-hidden="true">
              Password:
             </div> 
             <div> 
              <input id="password" onfocus="g_fFcs=0" name="password" value="" type="password" class="signInInputText" aria-labelledby="passwordLabel" /> 
             </div> 
             <div> 
              <input id="passwordText" onfocus="g_fFcs=0" name="passwordText" value="" style="display: none;" class="signInInputText" aria-labelledby="passwordLabel" /> 
             </div> 
             <div class="showPasswordCheck signInCheckBoxText"> 
              <input type="checkbox" id="showPasswordCheck" class="chk" onclick="showPasswordClick()" /> 
              <span>Show password</span> 
             </div> 
            </div> 

I'm able to find the element using input id for username and password and pass my static credentials in to it using the below code. I want my code to find these elements dynamically during the runtime using some keywords and pass the static credentials. How would i do that. I may not know the element id, text or xpath during run time it can be anything. Kindly suggest how this can be done or help me with a working example.

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.w3c.dom.Document;


public class Dynamic_LoginPass {

    public static final String Username = "admin";

    public static final String Password = "admin";

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","D:\\Eclipse\\workspace\\PopUp_Test\\lib\\chromedriver.exe");


        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://www.nature.com/");
         try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String windowHandle = driver.getWindowHandle();
            Assert.assertNotNull(windowHandle);

        Thread.sleep(10000);

        //driver.findElement(By.xpath("//*[@id=\"easycont\"]/div/div[2]/div[2]/div[2]/button")).click();
        String html_content1 = driver.getPageSource();


         // Jsoup makes DOM here by parsing HTML content
         org.jsoup.nodes.Document doc1 = Jsoup.parse(html_content1);
         System.out.println("Result:"+doc1);

         int test= doc1.select("div:contains(signInInputLabel)").size();
         System.out.println("testtesttesttest::"+test);

            if(driver.findElement(By.cssSelector("input[id=username]")) != null){     
                driver.findElement(By.cssSelector("input[id=username]")).sendKeys(Username); 
                driver.findElement(By.cssSelector("input[id=password]")).sendKeys(Password);
                driver.findElement(By.xpath("//*[@id=\"bglogodiv\"]/div[7]/div/span")).click();
                System.out.println("#############clicked################"); 
            } else if(driver.findElement(By.cssSelector("input[id=password]")) != null){ 

                System.out.println("clicked in else if");   
            }    


    }
}

Thanks in advance

syndy1989
  • 403
  • 10
  • 25

1 Answers1

0

In my opinion the best way to use Jsoup FormElement class. You can check how this should be done. In this example

This practical approach use only jsoup. Therefore no need for WebDriver.

kvizer
  • 13
  • 3