我想使用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>
我能够使用输入ID为用户名和密码找到该元素,并使用以下代码将我的静态凭据传递给它。我希望我的代码在运行时使用一些关键字动态查找这些元素并传递静态凭据。我该怎么做在运行时它可能不知道元素id,text或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");
}
}
}
提前致谢
答案 0 :(得分:0)
在我看来,使用Jsoup FormElement类的最佳方法。 你可以检查一下应该怎么做。 In this example
这种实用方法仅使用jsoup。因此不需要WebDriver。