<div id="address" class="guideFieldDescription short" style="null;display:none">
<p>
Enter home address for the contact person. Leave blank if you don't have one. Home Address and Mailing Address must be located in the United States. No international addresses will be accepted. If the home addresses and mailing addresses are different, you must fill out both sections.<br>
</p>
我正在尝试获取
标记的内容,但是使用下面的脚本
会得到null或为空WebElement WebElement = driver.findElement(By.xpath("//*[@id='address']"));
List<WebElement> paras = WebElement.findElements(By.tagName("p"));
System.out.println("Size = " + paras.size() + " " + paras.toString() );
for (WebElement para : paras) {
System.out.println(para.getText());}
我得到的大小为= 1,但getText()返回空。
答案 0 :(得分:1)
硒getText()
无法从包含display: none
及其子div
段落的p
元素中获取文本。如果元素可见(未设置为display: none;
),则您的代码将正常工作。
如果元素不可见,则可以使用getText()
来获取JavascriptExecutor
来代替innerText
。看到以下可能重复的问题:Using selenium, can I get the text of an invisible element?
这是一个获取内部文本的函数
/**
* Get the innerText from an element.
* @param driver the WebDriver
* @param element the element to get innerText from
* @return the element's innerText
*/
public static String getInnerText(WebDriver driver, WebElement element) {
JavascriptExecutor executor = (JavascriptExecutor) driver;
return (String) executor.executeScript("return arguments[0].innerText", element);
}
下面的代码示例中使用了此功能。
要查看getText()
与获得innerText
可见元素和不可见元素之间的区别,下面是一个完整的示例程序(注意中间的步骤,以调试并添加{{ 1}}手动):
display: none
该程序的输出应如下所示
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
class DemonstrateGetTextVsGetInnerTextForDisplayNoneElements {
public static void main(final String[] args) {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
// Let's go to a page that mirrors the use case of a div container,
// with children paragraphs.
// The difference: this page doesn't have display: none set on the container.
driver.get("https://www.google.com/intl/en/about/");
final WebElement container = driver.findElement(By.className("home-hero-copy"));
final List<WebElement> paragraphs = container.findElements(By.tagName("p"));
System.out.println("getText() works as normal for *VISIBLE* containers and paragraphs.");
System.out.println("CONTAINER: " + container.getText());
System.out.println(
"LIST OF PARAGRAPHS, Size = " + paragraphs.size() + " " + paragraphs.toString());
for (final WebElement paragraph : paragraphs) {
System.out.println("PARAGRAPH: " + paragraph.getText());
}
System.out.println("SET THE JAVA DEBUGGER TO PAUSE RIGHT HERE, "
+ "GO INTO THE BROWSER AND INJECT \"display: none;\" "
+ "as a style on the div.home-hero-copy element to make"
+ "the div and its child paragraphs invisible. "
+ "You can do this by using the developer tools elements panel.");
System.out.println("If you've made the container invisible, "
+ "you should notice that in the following block of printouts "
+ "that we've still got references to the WebElements (they aren't stale) "
+ "but when we try to getText() while they are invisible from 'display: none;', "
+ "we won't get any text back.");
System.out.println("CONTAINER: " + container.getText());
System.out.println(
"LIST OF PARAGRAPHS, Size = " + paragraphs.size() + " " + paragraphs.toString());
for (final WebElement paragraph : paragraphs) {
System.out.println("PARAGRAPH: " + paragraph.getText());
}
System.out.println("Now, let's try getting the text via 'innerText' with a Javascript Executor");
System.out.println("CONTAINER: " + getInnerText(driver, container));
System.out.println(
"LIST OF PARAGRAPHS, Size = " + paragraphs.size() + " " + paragraphs.toString());
for (final WebElement paragraph : paragraphs) {
System.out.println("PARAGRAPH: " + getInnerText(driver, paragraph));
}
System.out.println("As you can see, getting inner text works when the element is invisible!");
driver.quit();
}
/**
* Get the innerText from an element.
* @param driver the WebDriver
* @param element the element to get innerText from
* @return the element's innerText
*/
public static String getInnerText(WebDriver driver, WebElement element) {
JavascriptExecutor executor = (JavascriptExecutor) driver;
return (String) executor.executeScript("return arguments[0].innerText", element);
}
}
以防万一Google更改了页面,这是div及其子级的样子:
Connected to the target VM, address: '127.0.0.1:62943', transport: 'socket'
Starting ChromeDriver 71.0.3578.33 (269aa0e3f0db08097f0fe231c7e6be200b6939f7) on port 15369
Only local connections are allowed.
Nov 13, 2018 11:07:46 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
getText() works as normal for *VISIBLE* containers and paragraphs.
CONTAINER: Our mission is to organize the world’s information and make it universally accessible and useful.
LIST OF PARAGRAPHS, Size = 1 [[[[[ChromeDriver: chrome on MAC (bbb9840f94250510047ac8e04b055d88)] -> class name: home-hero-copy]] -> tag name: p]]
PARAGRAPH: Our mission is to organize the world’s information and make it universally accessible and useful.
SET THE JAVA DEBUGGER TO PAUSE RIGHT HERE, GO INTO THE BROWSER AND INJECT "display: none;" as a style on the div.home-hero-copy element to makethe div and its child paragraphs invisible. You can do this by using the developer tools elements panel.
If you've made the container invisible, you should notice that in the following block of printouts that we've still got references to the WebElements (they aren't stale) but when we try to getText() while they are invisible from 'display: none;', we won't get any text back.
CONTAINER:
LIST OF PARAGRAPHS, Size = 1 [[[[[ChromeDriver: chrome on MAC (bbb9840f94250510047ac8e04b055d88)] -> class name: home-hero-copy]] -> tag name: p]]
PARAGRAPH:
Now, let's try getting the text via 'innerText' with a Javascript Executor
CONTAINER: Our mission is to organize the world’s information and make it universally accessible and useful.
LIST OF PARAGRAPHS, Size = 1 [[[[[ChromeDriver: chrome on MAC (bbb9840f94250510047ac8e04b055d88)] -> class name: home-hero-copy]] -> tag name: p]]
PARAGRAPH: Our mission is to organize the world’s information and make it universally accessible and useful.
As you can see, getting inner text works when the element is invisible!
Disconnected from the target VM, address: '127.0.0.1:62943', transport: 'socket'
Process finished with exit code 0