打开的是一个国外的网站,不行就用vpn翻个墙先,直接运行看下什么效果就行,不知怎么言传,大概就是控制浏览器去做一些重复性的工作!
#encoding=utf-8 #python+selenium自动化测试by@bigwayseo.com from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.proxy import * import time,re #以上皆为导入模块https://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()