使用Jsoup查找具有特定文本的元素
问题描述:
我想使用JSoup从HTML中选择具有特定文本的元素。 HTML是使用Jsoup查找具有特定文本的元素
<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>
我能找到使用输入id的用户名和密码的元素,并使用下面的代码将我的静态证书传递给它。我希望我的代码在运行时使用一些关键字动态地查找这些元素,并传递静态凭据。我将如何做到这一点。我可能不知道运行时的元素ID,文本或xpath,它可以是任何东西。请建议如何做到这一点,或者以一个实例来帮助我。
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");
}
}
}
在此先感谢
请缩小HTML和代码的具体问题。 –
@ M.leRutte请找到更新的HTML和代码 – syndy1989