繁体   English   中英

得到<p>使用 Selenium webdriver 的内容 - Java

[英]Get <p> content using Selenium webdriver - Java

<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() 返回空。

Selenium getText()无法从带有display: none元素中获取文本,其中包括div及其子p段落。 如果元素可见(未设置为display: none; ),则您的代码将正常工作。

如果元素不可见,您可以使用JavascriptExecutor来获取元素的innerText而不是使用getText() 看到这个可能重复的问题: 使用硒,我可以得到一个不可见元素的文本吗?

这是一个获取内部文本的函数

/**
 * 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之间的区别,这里是一个完整的工作示例程序(注意中间的步骤调试和添加display: none手动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);
    }
}

这个程序的输出应该是这样的

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

以防万一谷歌改变他们的页面,这里是 div 和它的孩子的样子:

<div class="home-hero-copy center">
    <p>Our mission is to <span class="color-hero">organize</span> the world’s <span class="color-hero">information</span> and make it <span class="color-hero">universally accessible</span> and <span class="color-hero">useful</span>.</p>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM