相关环境搭建我会抽空写篇博客来介绍,
这里直接附上源代码和效果视频。
ps:这里我是真不知道怎么传视频,只好放gif图
源代码:
from selenium import webdriver
import time
import random
from selenium.webdriver.remote import switch_to
from selenium.webdriver.support.expected_conditions import frame_to_be_available_and_switch_to_it
from test.test_ssl import handle_error
d=webdriver.Chrome()
d.get('http://127.0.0.1/phpwind/index.php')
print(d.title)
#登录
d.find_element_by_id('J_username').send_keys("admin")
time.sleep(3)
d.find_element_by_id('J_password').send_keys('123456')
time.sleep(3)
d.find_element_by_id("J_sidebar_login").click()
time.sleep(3)
#随机验证码
def yzm(len=6):
code_list = []
for i in range(10): # 0-9数字
code_list.append(str(i))
for i in range(65, 91): # 对应从“A”到“Z”的ASCII码
code_list.append(chr(i))
for i in range(97, 123): #对应从“a”到“z”的ASCII码
code_list.append(chr(i))
myslice = random.sample(code_list, len) # 从list中随机获取6个元素,作为一个片断返回
verification_code = ''.join(myslice) # list to string
return verification_code
#发帖
d.find_element_by_link_text('版块').click()
time.sleep(3)
d.find_element_by_link_text('新版块').click()
time.sleep(3)
d.find_element_by_xpath('/html/body/div/div[2]/div[2]/div[2]/div/div[3]/a').click()
time.sleep(3)
d.find_element_by_id('J_atc_title').send_keys('我爱学习'+yzm())
time.sleep(3)
d.switch_to.frame(d.find_elements_by_tag_name("iframe")[0])
d.find_element_by_xpath('/html/body').send_keys('太阳当空照,花儿对我笑'+yzm(len=20))
d.switch_to_default_content()
time.sleep(3)
d.find_element_by_name('Submit').click()
time.sleep(10)
d.quit()