一个研究Python实践,最近研究一个投票的东东,主要是想测试利用Python实现刷微信投票。
本文纯粹为了记录一下 webdriver直接操作页面按钮的方法:
#!/usr/bin/python #coding=utf-8 from selenium import webdriver from selenium.webdriver import DesiredCapabilities import sys,time reload(sys) sys.setdefaultencoding(‘utf-8‘) desired_capabilities= DesiredCapabilities.PHANTOMJS.copy() headers = {‘Accept‘: ‘*/*‘, ‘Accept-Language‘: ‘en-US,en;q=0.8‘, ‘Cache-Control‘: ‘max-age=0‘, ‘User-Agent‘: ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36‘,#这种修改 UA 也有效 ‘Connection‘: ‘keep-alive‘ } for key, value in headers.iteritems(): desired_capabilities[‘phantomjs.page.customHeaders.{}‘.format(key)] = value desired_capabilities[‘phantomjs.page.customHeaders.User-Agent‘] =‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36‘ driver= webdriver.PhantomJS(desired_capabilities=desired_capabilities) url=‘‘ driver.get(url) js = "var q=document.body.scrollTop=100000" for i in range(10): driver.execute_script(js) time.sleep(0.1) print driver.find_element_by_xpath(‘//*[@id="201290753"]/div[2]/ul/li[1]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290753"]/div[2]/ul/li[1]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290816"]/div[2]/ul/li[2]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290816"]/div[2]/ul/li[2]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290817"]/div[2]/ul/li[1]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290817"]/div[2]/ul/li[1]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290817"]/div[2]/ul/li[2]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290817"]/div[2]/ul/li[2]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290817"]/div[2]/ul/li[3]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290817"]/div[2]/ul/li[3]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290818"]/div[2]/ul/li[1]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290818"]/div[2]/ul/li[1]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290818"]/div[2]/ul/li[2]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290818"]/div[2]/ul/li[2]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290818"]/div[2]/ul/li[3]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290818"]/div[2]/ul/li[3]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290818"]/div[2]/ul/li[4]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290818"]/div[2]/ul/li[4]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290820"]/div[2]/ul/li[1]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290820"]/div[2]/ul/li[1]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290820"]/div[2]/ul/li[2]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290820"]/div[2]/ul/li[2]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290821"]/div[2]/ul/li[4]/p/span‘).text driver.find_element_by_xpath(‘//*[@id="201290821"]/div[2]/ul/li[4]/p/span‘).click() print ‘zhesHi 11111 777777777777777777777‘ print driver.find_element_by_xpath(‘//*[@id="201290822"]/div[2]/ul/li[1]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290822"]/div[2]/ul/li[1]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290822"]/div[2]/ul/li[2]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290822"]/div[2]/ul/li[2]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290824"]/div[2]/ul/li[2]/p/span/span‘).text driver.find_element_by_xpath(‘//*[@id="201290824"]/div[2]/ul/li[2]/p/span/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290825"]/div[2]/ul/li[2]/p/span‘).text driver.find_element_by_xpath(‘//*[@id="201290825"]/div[2]/ul/li[2]/p/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290826"]/div[2]/ul/li[2]/p/span‘).text driver.find_element_by_xpath(‘//*[@id="201290826"]/div[2]/ul/li[2]/p/span‘).click() print driver.find_element_by_xpath(‘//*[@id="201290841"]/div[2]/div/div/input‘).text driver.find_element_by_xpath(‘//*[@id="201290841"]/div[2]/div/div/input‘).send_keys(u‘我是哥哥‘) print driver.find_element_by_xpath(‘//*[@id="201290841"]/div[2]/div/div/input‘).text print driver.find_element_by_xpath(‘//*[@id="form_submit"]‘).text driver.find_element_by_xpath(‘//*[@id="form_submit"]‘).click() time.sleep(2) print driver.page_source driver.quit()
以上就是我的实践,更多可以参考:tp.gam7.com