一个研究Python实践,最近研究一个投票的东东,主要是想测试利用Python实现刷微信投票。
本文纯粹为了记录一下 webdriver直接操作页面按钮的方法:
#!/usr/bin/python
#coding=utf-8
from selenium import webdriver
from import DesiredCapabilities
import sys,time
reload(sys)
('utf-8')
desired_capabilities= ()
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 ():
desired_capabilities['.{}'.format(key)] = value
desired_capabilities['-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= (desired_capabilities=desired_capabilities)
url=''
(url)
js = "var q==100000"
for i in range(10):
driver.execute_script(js)
(0.1)
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[3]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[3]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[3]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[3]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[4]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[4]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[4]/p/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[4]/p/span').click()
print 'zhesHi 11111 777777777777777777777'
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[1]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span').text
driver.find_element_by_xpath('//*[@]/div[2]/ul/li[2]/p/span').click()
print driver.find_element_by_xpath('//*[@]/div[2]/div/div/input').text
driver.find_element_by_xpath('//*[@]/div[2]/div/div/input').send_keys(u'我是哥哥')
print driver.find_element_by_xpath('//*[@]/div[2]/div/div/input').text
print driver.find_element_by_xpath('//*[@]').text
driver.find_element_by_xpath('//*[@]').click()
(2)
print driver.page_source
()
以上就是我的实践,更多可以参考:tp.