51Testing软件测试论坛
标题:
【求助】python+selenium,求达人帮忙解惑
[打印本页]
作者:
zk_testing
时间:
2011-11-9 19:46
标题:
【求助】python+selenium,求达人帮忙解惑
下面代码是一段点击页面链接(新建窗口打开链接),然后验证其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()
作者:
流光
时间:
2011-11-24 11:21
1、加一下 等待时间呢? time.sleep()
2、ASCII报错的问题看看 在带有中文的字符串前面加个 u 像这样string=u'中文字符串'
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2