这是我学习Selenium的第二天。我想在这些html标签之间提取文本。
HTML代码示例:
<div id="media-buttons" class="hide-if-no-js"/>
<textarea id="DescpRaw" class="ckeditor" name="DescpRaw" rows="13" cols="100" style="visibility: hidden; display: none;">
Cactus spines are produced from specialized structures
called areoles, a kind of highly reduced branch. Areoles
are an identifying feature of cacti.
</textarea>
</div>
所需结果:
Cactus spines are produced from specialized structures
called areoles, a kind of highly reduced branch. Areoles
are an identifying feature of cacti.
我在下面尝试过Selenium驱动程序,但它是空的。
String bodyhtml = driver.findElement(By.xpath("//textarea[@name='DescpRaw']")).getText();
谢谢!
答案 0 :(得分:0)
String bodyhtml = driver.findElement(By.xpath("//textarea[@name='DescpRaw']")).getAttribute("innerHTML");
我也建议使用ID,因为它可用且速度更快。
String bodyhtml = driver.findElement(By.id("DescpRaw")).getAttribute("innerHTML");
答案 1 :(得分:0)
有几件事......
如果元素有ID,则应始终使用该ID。根据HTML标准,它在页面上应该是唯一的,因此它是任何元素的理想标识符。
您遇到的问题是隐藏了TEXTAREA
。您可以在元素上告知此style="visibility: hidden; display: none;"
。 Selenium旨在以用户的身份与网页进行交互。任何不可见的元素,Selenium都不会与之交互。理想的情况是弄清楚如何公开或显示textarea字段...点击一些按钮/链接/其他和然后从中获取文本。使用TEXTAREA
字段,您可能需要.getAttribute("value")
元素。
使元素可见的几种替代方法是使用Javascript来抓取元素文本或使用其他人建议的.getAttribute("innerHTML")
。