为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Selenium webdriver - Java 获取 <p> 内容

使用 Selenium webdriver - Java 获取 <p> 内容

HUWWW 2022-01-19 13:07:44
<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() 返回空。
查看完整描述

1 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

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


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


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


/**

 * 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()和获取,这是一个完整的工作示例程序(注意中间的步骤以调试和手动添加):innerTextdisplay: 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>


查看完整回答
反对 回复 2022-01-19
  • 1 回答
  • 0 关注
  • 532 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号