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

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


HUWWW
浏览 533回答 1
1回答

Cats萌萌

SeleniumgetText()无法从display: none包含div及其子p段落的元素中获取文本。如果元素可见(未设置为display: none;),则您的代码将正常工作。如果元素不可见getText(),您可以使用 aJavascriptExecutor来获取元素的,而不是使用。innerText看到这个可能重复的问题:使用硒,我可以得到一个不可见元素的文本吗?这是一个获取内部文本的函数/**&nbsp;* Get the innerText from an element.&nbsp;* @param driver&nbsp; &nbsp; the WebDriver&nbsp;* @param element&nbsp; &nbsp;the element to get innerText from&nbsp;* @return&nbsp; the element's innerText&nbsp;*/public static String getInnerText(WebDriver driver, WebElement element) {&nbsp; &nbsp; JavascriptExecutor executor = (JavascriptExecutor) driver;&nbsp; &nbsp; return (String) executor.executeScript("return arguments[0].innerText", element);}此函数用于以下代码示例。要查看可见元素和不可见元素之间的区别getText()和获取,这是一个完整的工作示例程序(注意中间的步骤以调试和手动添加):innerTextdisplay: noneimport 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 {&nbsp; &nbsp; public static void main(final String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; WebDriverManager.chromedriver().setup();&nbsp; &nbsp; &nbsp; &nbsp; WebDriver driver = new ChromeDriver();&nbsp; &nbsp; &nbsp; &nbsp; // Let's go to a page that mirrors the use case of a div container,&nbsp; &nbsp; &nbsp; &nbsp; // with children paragraphs.&nbsp; &nbsp; &nbsp; &nbsp; // The difference: this page doesn't have display: none set on the container.&nbsp; &nbsp; &nbsp; &nbsp; driver.get("https://www.google.com/intl/en/about/");&nbsp; &nbsp; &nbsp; &nbsp; final WebElement container = driver.findElement(By.className("home-hero-copy"));&nbsp; &nbsp; &nbsp; &nbsp; final List<WebElement> paragraphs = container.findElements(By.tagName("p"));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("getText() works as normal for *VISIBLE* containers and paragraphs.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("CONTAINER: " + container.getText());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "LIST OF PARAGRAPHS, Size = " + paragraphs.size() + "&nbsp; &nbsp;" + paragraphs.toString());&nbsp; &nbsp; &nbsp; &nbsp; for (final WebElement paragraph : paragraphs) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("PARAGRAPH: " + paragraph.getText());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("SET THE JAVA DEBUGGER TO PAUSE RIGHT HERE, "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "GO INTO THE BROWSER AND INJECT \"display: none;\" "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "as a style on the div.home-hero-copy element to make"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "the div and its child paragraphs invisible. "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "You can do this by using the developer tools elements panel.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("If you've made the container invisible, "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "you should notice that in the following block of printouts "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "that we've still got references to the WebElements (they aren't stale) "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "but when we try to getText() while they are invisible from 'display: none;', "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "we won't get any text back.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("CONTAINER: " + container.getText());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "LIST OF PARAGRAPHS, Size = " + paragraphs.size() + "&nbsp; &nbsp;" + paragraphs.toString());&nbsp; &nbsp; &nbsp; &nbsp; for (final WebElement paragraph : paragraphs) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("PARAGRAPH: " + paragraph.getText());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Now, let's try getting the text via 'innerText' with a Javascript Executor");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("CONTAINER: " + getInnerText(driver, container));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "LIST OF PARAGRAPHS, Size = " + paragraphs.size() + "&nbsp; &nbsp;" + paragraphs.toString());&nbsp; &nbsp; &nbsp; &nbsp; for (final WebElement paragraph : paragraphs) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("PARAGRAPH: " + getInnerText(driver, paragraph));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("As you can see, getting inner text works when the element is invisible!");&nbsp; &nbsp; &nbsp; &nbsp; driver.quit();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Get the innerText from an element.&nbsp; &nbsp; &nbsp;* @param driver&nbsp; &nbsp; the WebDriver&nbsp; &nbsp; &nbsp;* @param element&nbsp; &nbsp;the element to get innerText from&nbsp; &nbsp; &nbsp;* @return&nbsp; the element's innerText&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static String getInnerText(WebDriver driver, WebElement element) {&nbsp; &nbsp; &nbsp; &nbsp; JavascriptExecutor executor = (JavascriptExecutor) driver;&nbsp; &nbsp; &nbsp; &nbsp; return (String) executor.executeScript("return arguments[0].innerText", element);&nbsp; &nbsp; }}这个程序的输出应该是这样的Connected to the target VM, address: '127.0.0.1:62943', transport: 'socket'Starting ChromeDriver 71.0.3578.33 (269aa0e3f0db08097f0fe231c7e6be200b6939f7) on port 15369Only local connections are allowed.Nov 13, 2018 11:07:46 AM org.openqa.selenium.remote.ProtocolHandshake createSessionINFO: Detected dialect: OSSgetText() 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&nbsp; &nbsp;[[[[[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:&nbsp;LIST OF PARAGRAPHS, Size = 1&nbsp; &nbsp;[[[[[ChromeDriver: chrome on MAC (bbb9840f94250510047ac8e04b055d88)] -> class name: home-hero-copy]] -> tag name: p]]PARAGRAPH:&nbsp;Now, let's try getting the text via 'innerText' with a Javascript ExecutorCONTAINER: Our mission is to organize the world’s information and make it universally accessible and useful.LIST OF PARAGRAPHS, Size = 1&nbsp; &nbsp;[[[[[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">&nbsp; &nbsp; <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>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java