[英]parse HTML javascript to JAVA using selenium
我正在尝试使用硒获取h1的值作为字符串。
这是HTML javascript-
<script type="text/javascript">
$(window).load(function() {
var $windowHeight = $(window).height() -12;
$("#top").height($windowHeight);
$('h1').css({
'margin-top' : (($windowHeight) - $('h1').outerHeight())/2,
'margin-bottom' : (($windowHeight) - $('h1').outerHeight())/2,
'opacity' : '1.0',
'filter' : 'alpha(opacity = 100)',
});
$("#container").click(function(){
$("html, body").animate({
scrollTop: $windowHeight + 50
}, 1500);
})
});
$(window).on("debouncedresize", function( event ) {
var $windowHeight = $(window).height() -12;
$("#top").height($windowHeight);
$('h1').css({
'margin-top' : (($windowHeight) - $('h1').outerHeight())/2,
'margin-bottom' : (($windowHeight) - $('h1').outerHeight())/2
});
});
</script>
这是我用JAVA编写的
WebDriver driver = new FirefoxDriver();
driver.get("view-source:http://websitename.com/");
Thread.sleep(3000);
JavascriptExecutor js = null;
if (driver instanceof JavascriptExecutor) {
js = (JavascriptExecutor)driver;
}
js.executeScript("h1");
不知道我是否应该首先使用JavascriptExecutor。 我将不胜感激。 谢谢
h1是页面上的标签。 为什么您尝试使用JavascriptExecutor访问它? 如果要获取标题h1的文本,只需使用以下代码
String text = driver.findElement(By.css("h1")).getText();
如果要获取标签的属性,请使用此代码
String attr= driver.findElement(By.css("h1")).getAttribute(<attr-name>);
现在可以使用! 我应该使用driver.getPageSource()获取页面源。 不是通过driver.get(“ view-source:websitename.com/”)。 愚蠢的我。 谢谢您的帮助! :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.