[英]Get Text with Selenium Webdriver in Java
I need to extract text Paul Robert this html and print to the console using the Java WebDriver and selenium . 我需要提取HTML文本Paul Robert并使用Java WebDriver和selenium打印到控制台。
The code below extracts all text Div , even the text " Quit". 下面的代码提取所有文本Div,甚至文本“ Quit”。
<div role="alert" class="alert success" id="control_gen_3">
<p>
<strong>Invite to <a href="">Paul Robert</a>.</strong></p>
<button class="dismiss" id="global-error-dismiss">Quit</button>
</div>
Code Selenium: 代码硒:
String pessoa = driver.findElement(By.id("control_gen_3")).getText();
System.out.println(pessoa );
String pessoa = driver.findElement(By.xpath("//div[@id='control_gen_3']//a")).getText();
System.out.println(pessoa );
Hope this helps you.. 希望这对您有帮助。
String pessoa = driver.findElement(By.cssSelector("#control_gen_3 a")).getText();
System.out.println(pessoa);
This means you fetch the element with id control_gen_3
and look for an a
within that element. 这意味着您获取ID为
control_gen_3
的元素,并在该元素中寻找a
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.