我正在尝试使用 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>);
关于java - 使用 selenium 将 HTML javascript 解析为 JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17460471/