|
下面代码是一段点击页面链接(新建窗口打开链接),然后验证其URL的代码。
在运行的时候总是无法成功。请高手解答;
问题:
1、有时候运行的时候,close()先于assertEqual运行,关闭了页面,导致不成功;
2、有时运行会报ASCII错误。但我头部已经定义了utf8。。。
3、有时候(几率很大)选择第二个窗口后,get_location()还是会获取前一个窗口的URL
这是为何?
Python code
#coding=utf8
from selenium import *
import unittest,time,re,os,logging
class a(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "http://www.testhome.com")
self.selenium.start()
def test_click(self):
sel = self.selenium
port_NewWindow_Link_Xpath = [["xpath=/html/body/div[2]/div/ul/li[3]/a","http://www.test1.com","title1"],
["xpath=/html/body/div[2]/div/ul/li/a","http://www.test2.com","title2"],
["xpath=/html/body/div[2]/div/ul[2]/li[8]/a","http://www.test3.com","title3"]]
sel.open("/")
#logging.info("新建页面链接-->开始。")
for click_link_NewWindow in port_NewWindow_Link_Xpath: #迭代器,遍历所有数据(Xpath,URL)
#try:
NewWindow_Link_Xpath = click_link_NewWindow[0]
NewWindow_Link_link_URL = click_link_NewWindow[1]
NewWindow_Link_link_title = click_link_NewWindow[2]
print(NewWindow_Link_link_title)
sel.click(NewWindow_Link_Xpath)
sel.select_window(unichr(NewWindow_Link_link_title))
self.assertEqual(sel.get_location(),NewWindow_Link_link_URL)
print("sel.get_location():",sel.get_location(),"NewWindow_Link_link_URL:",NewWindow_Link_link_URL)
sel.close()
#sel.select_window(sel.get_all_window_titles()[0])
#except:
#logging.error('action error:click',)
#logging.error('error xpath:',NewWindow_Link_Xpath,NewWindow_Link_link_URL)
sel.open("/")
#logging.info("新建页面链接-->结束。")
def test_teardown(self):
self.selenium.close()
self.selenium.stop()
self.selenium.shut_down_selenium_server()
if __name__ == '__main__':
unittest.main() |
|