【求助】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/div/ul/li/a","http://www.test1.com","title1"],
["xpath=/html/body/div/div/ul/li/a","http://www.test2.com","title2"],
["xpath=/html/body/div/div/ul/li/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
NewWindow_Link_link_URL = click_link_NewWindow
NewWindow_Link_link_title = click_link_NewWindow
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())
#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() 1、加一下 等待时间呢?time.sleep()
2、ASCII报错的问题看看 在带有中文的字符串前面加个 u像这样string=u'中文字符串'
页:
[1]