我的selenium python测试代码没有引发element not visible异常

2024-06-01 09:55:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我是用python学习selenium的新手。在这里,我编写代码来测试我的html页面(添加了延迟),我期望element not visible exception,但是我的测试代码说是success并关闭浏览器,尽管我已经对tearDown()函数进行了注释。我不明白这是怎么发生的?我的目标是用seleniumpython学习隐式和显式等待。你知道吗

下面是我的selenium python测试代码:

import unittest
from selenium import webdriver

class DemoTest(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Chrome("F:\Nalini_Backup\NALs\Selenium\chromedriver_win32\chromedriver.exe")
        self.driver.get("file:///F:/Nalini_Backup/NALs/Python/Selenium_Python/Workout/calc_num.html")   
        #self.driver.implicitly_wait(30)

    def test_func1(self):    
        var = self.driver.find_element_by_xpath("/html/body/button")
        var.click()
        var1= self.driver.find_element_by_xpath("//*[@id='demo']").tag_name
        print var1

        self.assertEqual(var1,"p","navigator.appName property check")


    #def tearDown(self):
        #self.driver.quit()    

if __name__ == "__main__":
    unittest.main(verbosity=2)

下面是我的HTML页面:

<html>
<head>
<title>Test for selenium waits</title>
</head>

<body>
<div align="center">
<h1>
<script>
    document.write("Hello World")
</script>
</h2>
</div>

<script>
    document.write("Beautiful day!!")
    if (navigator.appName == "Netscape")
    {   document.write("You are using Netscape");
        window.status = "status message is displayed here";
        <!--alert("Netscape detected");-->
    }
    else
    {
    document.write("you are not using chrome <br>");
    document.write("may be some other browser");
    window.alert("Netscape not detected");

    }
</script>

<p>Test button for browser name </p>
<button onclick="setTimeout(test,6000)"> Click me </button>
<p id ="demo"></p>
<script>
function test()
{

var x = navigator.appName
document.getElementById("demo").innerHTML=x
}
</script>
</body>
</html>

Tags: testselfvardefhtmldriverseleniumscript