打开新页面时,Selenium脚本无法找到功能

时间:2011-07-12 03:38:03

标签: python selenium-rc

在单独的函数中打开另一个页面时,我的selenium-python脚本出现问题。

在功能test2_points()中,selenium无法找到functionselectProducts_4

这是我的剧本:

from selenium import selenium
import unittest, time, re
import socket
import sys

class discount_reward(unittest.TestCase):
    def setUp(self):
         print "setUp invoked"
         self.port               = "4444"
         self.ip_address         = "localhost"
         self.browser            = "*chrome"#firefox
         self.base_url           = "http://128.0.0.1:1062"
         self.browser_title      = "Hammer"
         self.verificationErrors = []

         try:
            print "starting selenium with the following parameters:"
            print "ip address:  %s\nport:  %s\nbrowser:  %s\nbase_url:  %s" %(self.ip_address, self.port, self.browser, self.base_url)
            self.selenium = selenium(self.ip_address, self.port, self.browser, self.base_url)
            self.selenium.start()

         except socket.error, error:
            print "\nSocket Exception: %s" %(error)
            print "\nCould not connect to %s:%s with browser %s" %(self.base_url, self.port, self.browser)
            sys.exit(1)

    def test1_discount_reward(self):
        sel = self.selenium
        sel.open("/Hammer/offer-rew-discount.aspx?")
        sel.select("functionselect", "label=Products_2")
        sel.click("select1")
    def test2_points_reward(self):
        sel = self.selenium
        sel.open("/Hammer/offer-rew-points.aspx?")
        sel.select("functionselect", "label=Products_4")
        sel.click("select1")

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

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

谁能告诉我什么是错的?

1 个答案:

答案 0 :(得分:1)

在页面加载完成之前,您几乎肯定会执行sel.select()。每当您执行导致页面转换的操作时(例如open()click()在链接或按钮上),您应该使用wait_for_page_to_load(timeout_in_milliseconds)跟随它。当页面完全加载到浏览器中时,等待将结束,或者超时(这显然是一个错误条件)。