导出的Selenium脚本抛出异常

1 投票
1 回答
645 浏览
提问于 2025-04-16 23:27

我使用Selenium IDE录制了一个操作,然后通过文件菜单选择了“导出测试用例”为Python 2(远程控制)格式。

我正在使用Selenium RC 2.3.0。

接着,我在命令提示符下运行了这个脚本,但它抛出了一个异常。

导出的Python 2 RC代码是:

from selenium import selenium
import unittest, time, re

class login(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://testServer1/")
        self.selenium.start()

    def test_login(self):
        sel = self.selenium
        sel.open("/WebAccess/login.html")
        sel.type("id=LoginID", "Administrator")
        sel.type("id=Password", "Administrator")
        sel.click("id=login")
        sel.wait_for_page_to_load("30000")
        sel.click("link=Home") # This is where it fails.

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

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

在这一行 sel.click("link=Home") 时,它出现了异常。

======================================================================
ERROR: test_login (__main__.login)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Documents and Settings\User\Desktop\login.py", line 16, in test_log
in
    sel.click("link=Home")
  File "C:\Python27\lib\selenium\selenium.py", line 290, in click
    self.do_command("click", [locator,])
  File "C:\Python27\lib\selenium\selenium.py", line 217, in do_command
    raise Exception, data
Exception: ERROR: Element link=Home not found

----------------------------------------------------------------------

我甚至尝试把30000的超时时间增加到60000,但还是没有帮助。我可以看到页面完全加载了,并且因为超时时间更长而等待了一会儿。然而,当执行这个link=Home时,它还是失败了。

HTML代码是:

<div class="links nopreview">
<span><a class="csiAction" href="/WebAccess/home.html#URL=centric://REFLECTION/INSTANCE/_CS_Data/null">Home</a></span>
<span>&nbsp;•&nbsp;</span>
<span><span><a class="csiAction" href="/WebAccess/home.html#URL=centric://SITEADMIN/_CS_Site">Setup</a>
</span><span>&nbsp;•&nbsp;</span>
</span><span><a title="Sign Out" class="csiAction csiActionLink">Sign Out</a></span>
</div>

使用IDE录制的操作是:

<tr>
    <td>open</td>
    <td>/WebAccess/login.html</td>
    <td></td>
</tr>
<tr>
    <td>type</td>
    <td>id=LoginID</td>
    <td>Administrator</td>
</tr>
<tr>
    <td>type</td>
    <td>id=Password</td>
    <td>Administrator</td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>id=login</td>
    <td></td>
</tr>

<tr>
    <td>click</td>
    <td>link=Home</td>
    <td></td>
</tr>

1 个回答

0

试试用下面这样的XPath:

sel.click('//a[contains(text(),"Home")]')

撰写回答