python打開chrome瀏覽器自動登錄網站並發表說說批量

需要幾個模塊,xlrd,xlwt,xlutils,selenium,都可以用pip安裝。
打開命令提示行,pip install xlrd
其他類似。
如果pip命令失敗,則需要右鍵,我的電腦,屬性,高級系統設置,環境變量,path變量,在%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;D:\Program Files\AMD\ATI.ACE\Core-Static;後面添加
D:\Program Files\Python37\Scripts;。
需提前準備好excel表格,裏面填好用戶名,帳號,密碼,說說內容。如果只是自己的一個人的帳號,則可以在程序裏直接寫入。
另,由於要驅動chrome瀏覽器,需要一個驅動,地址https://sites.google.com/a/chromium.org/chromedriver/downloads
提示:chrome瀏覽器打開網頁後,右鍵,檢查,尋找關鍵詞,點擊一個剪頭的地方也可以直接定位按鈕之類的東西。尋找類似id的東西。
另,如果只是一個帳號的登錄,則簡單的多,只需要直接username和password賦值即可,不需要讀取excel的內容。xlrd,xlwt,xlutils都用不到。

import time
import xlrd
import xlwt
import time
from xlutils.copy import copy # as cp
from selenium import webdriver

workbook = xlrd.open_workbook(r'D:\s.xls')
copy = copy(workbook)      #複製
sheet1 = workbook.sheet_by_name('Sheet1')
row_count=sheet1.nrows  #獲取表格有效總行數
col_count= sheet1.ncols  #獲取表格有效總列數
#------------------------------------

for i in range(1,row_count):     #i從1到最後一行行數遍歷
            #username =str(sheet1.cell_value(i,1))   #獲取第一列每一個數據
            #password =sheet1.cell_value(i,2)
            username =str(sheet1.cell_value(i,1))
            password =str(sheet1.cell_value(i,2))
            shuoshuo = str(sheet1.cell_value(i,3))
            #因爲有很多用戶名要輸入,故用了循環,如果只有自己,則不用循環,上面的都可以去掉
            driver = webdriver.Chrome() # 選擇Chrome瀏覽器
            driver.get('https://www.rrrrr/') # 打首登錄頁面,此處略去網址
            time.sleep(1)

#找到用戶名輸入框點擊獲取焦點並輸入信息
            driver.find_element_by_id('loginForm_userName').click()
            driver.find_element_by_id('loginForm_userName').send_keys(username)

#找到密碼輸入框點擊獲取焦點並輸入信息
            driver.find_element_by_id('loginForm_password').click()
            driver.find_element_by_id('loginForm_password').send_keys(password)

# 找到圖形驗證碼輸入框點擊獲取焦點輸入信息
#driver.find_element_by_id('verifyCode').click()
#driver.find_element_by_id('verifyCode').send_keys(code)

# 找到登錄按鈕點擊
            driver.find_element_by_id('loginDialog').click()
            time.sleep(1)


#上面獲取個人中心按鈕,找不到按鈕,選擇直接跳轉的方式
            driver.get('https://www.lidddddddd')     #此處爲跳轉地址,省去
            time.sleep(1)

#點擊發表說說
            driver.find_element_by_id('spacefront_content').click()
            driver.find_element_by_id('spacefront_content').send_keys(shuoshuo)  #內容在excel中,也可直接給定

            driver.find_element_by_class_name('select2-search__field').click()

            driver.find_element_by_class_name('select2-results').click()
            driver.find_element_by_id('spacefront_publishPost').click()
            print(username)
            driver.quit()
       
        

參考帖子:
http://www.cnblogs.com/BigFishFly/p/6380024.html

https://www.jianshu.com/p/d7a966ec1189

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章