Node.js 使用Selenium测试按钮是否单击并使段落在phantomjs中消失

Node.js 使用Selenium测试按钮是否单击并使段落在phantomjs中消失,node.js,selenium,phantomjs,mocha.js,chai,Node.js,Selenium,Phantomjs,Mocha.js,Chai,所以现在我有一个超级简单的html文件,如果我点击一个按钮,一个段落就会消失。我试过了,效果不错。现在,我想通过在由Mocha和Chai组成的测试设置中运行phantomjs作为浏览器,并使用SeleniumWebDriver对其进行测试。我可以测试按钮是否有正确的文本,我可以设置它的CSS。我可以从每次测试结束时的屏幕截图中看出这一点。然而,每当我试图让按钮实际点击时,该段落仍然保留在屏幕截图中 这是我使用的行: driver.findElement(By.id('hide')).click

所以现在我有一个超级简单的html文件,如果我点击一个按钮,一个段落就会消失。我试过了,效果不错。现在,我想通过在由Mocha和Chai组成的测试设置中运行phantomjs作为浏览器,并使用SeleniumWebDriver对其进行测试。我可以测试按钮是否有正确的文本,我可以设置它的CSS。我可以从每次测试结束时的屏幕截图中看出这一点。然而,每当我试图让按钮实际点击时,该段落仍然保留在屏幕截图中

这是我使用的行:

 driver.findElement(By.id('hide')).click()
但它似乎不起作用。 此外,当我试图检查段落是否存在时,我会收到大量错误

assert.equal(element.getAttribute(), ":hidden", "Paragraph hidden when button clicked");
我非常了解这一点,因此,如果有人能告诉我如何或向我指出一些文档,我将不胜感激

这是我的HTML代码

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#hide").click(function(){
        console.log("Hiding");
        $("#hide").text("Done somethign")
        $("p").hide();
    });
    $("#show").click(function(){
        $("p").show();
    });
});
</script>
</head>
<body>

<h1>Simple Heading to Test</h1>
<p>If you click on the "Hide" button, I will disappear.</p>
<button id="hide">Hide</button>
<button id="show">Show</button>

</body>
</html>

测试段落是否可见:

element = driver.find_element_by_id('Paragraph') 
if element.is_displayed():
    print "Paragraph found"
else:
    print "Paragraph not found"

至于按钮,你能公布你得到的错误是什么吗?您可以发布按钮的html吗?

我已经添加了一些代码。并不是因为我出错,只是屏幕截图没有更新以显示按钮已被单击。而不是driver.findElement(webdriver.By.xpath(“/html/body/p”)。click()尝试driver.findElement(webdriver.By.id(“hide”)。click()
element = driver.find_element_by_id('Paragraph') 
if element.is_displayed():
    print "Paragraph found"
else:
    print "Paragraph not found"