Python+selenium自动化测试

打开的是一个国外的网站,不行就用vpn翻个墙先,直接运行看下什么效果就行,不知怎么言传,大概就是控制浏览器去做一些重复性的工作!
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#encoding=utf-8#python+selenium自动化测试by@bigwayseo.comfrom selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.proxy import *import time,re#以上皆为导入模块http://bigwayseo.com/#要控制的浏览器是火狐driver=webdriver.Firefox()# driver=webdriver.PhantomJS(executable_path='C:/Python27/phantomjs-2.0.0-windows/bin/phantomjs')#目标网站driver.get('https://www.styleseat.com/')#智能等待driver.implicitly_wait(5)#先清楚搜索框的东西driver.find_element_by_id('searchWord').clear()#输入关键词,按需可加上for循环做成批量driver.find_element_by_id('searchWord').send_keys('beauty')driver.implicitly_wait(5)#确认driver.find_element_by_id("searchButton").send_keys(Keys.ENTER)#定位元素res=driver.find_elements_by_xpath("//h3[@class='resultTitle']")#获取当前窗口nowhandle=driver.current_window_handle#遍历for i in res: try: i.find_element_by_xpath('a').click() driver.implicitly_wait(5) except Exception, exception: print exception continue #获取所有窗口 allhandles=driver.window_handles #循环判断时候是否为当前窗口nowhandle,不是就窗口都逐个去定位元素然后发个hello world for handle in allhandles: if handle!=nowhandle: driver.switch_to_window(handle) try: # driver.find_element_by_css_selector("a.left").click() driver.find_element_by_xpath("//a[contains(text(),'Message Me')]").click() time.sleep(5) driver.find_element_by_id('id_message').send_keys('hello world') driver.find_element_by_id('id_message_submit').click() time.sleep(5) except Exception, exception: print exception continue #只关闭当前窗口 driver.close() #回到窗口nowhandle driver.switch_to_window(nowhandle)#退出所有窗口driver.quit() |

浙公网安备 33010602011771号