这是我学习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();
谢谢!
发布于 2017-07-21 20:44:14
String bodyhtml = driver.findElement(By.xpath("//textarea[@name='DescpRaw']")).getAttribute("innerHTML");
另外,我推荐使用ID,因为它是可用的,而且速度更快。
String bodyhtml = driver.findElement(By.id("DescpRaw")).getAttribute("innerHTML");
发布于 2017-07-22 05:05:46
有几件事。
TEXTAREA
是隐藏的。给定元素上的style="visibility: hidden; display: none;"
,您就可以看出这一点。Selenium被设计成像用户一样与网页交互。任何看不见的元素,硒都不会与之相互作用。理想的情况是弄清楚如何公开或使文本区域字段可见……单击某个按钮/链接/任何内容,然后从其中获取文本。对于TEXTAREA
字段,您可能需要对元素执行.getAttribute("value")
操作。使元素可见的两种替代方法是使用Javascript获取元素文本,或者按照其他人的建议使用.getAttribute("innerHTML")
。
https://stackoverflow.com/questions/45245788
复制