Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jsoup查找具有特定文本的元素_Java_Selenium Webdriver - Fatal编程技术网

Java 使用Jsoup查找具有特定文本的元素

Java 使用Jsoup查找具有特定文本的元素,java,selenium-webdriver,Java,Selenium Webdriver,我想使用JSoup从HTML中选择一个具有特定文本的元素。HTML是 <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.j

我想使用JSoup从HTML中选择一个具有特定文本的元素。HTML是

        <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> 

提前感谢

在我看来,使用Jsoup类的最佳方式。 您可以检查应如何执行此操作。


这种实用的方法只使用jsoup。因此不需要WebDriver。

请将HTML和代码缩小到特定问题。@M.leRutte请查找更新的HTML和代码
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");   
            }    


    }
}