Skip to content

Commit

Permalink
selenium套件更新
Browse files Browse the repository at this point in the history
將套件更新到4.4.3版本,因此寫法全部都更新過
  • Loading branch information
rifleak74 authored Sep 23, 2022
1 parent da9bc91 commit 5b9eb75
Showing 1 changed file with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
第七章 FB自動發社團發文小幫手
分享小幫手
"""
from selenium.webdriver import DesiredCapabilities
# selenium,2022/9/17 將套件更新到4.4.3版本,因此寫法全部都更新過
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
import time
import random

Expand All @@ -21,55 +24,51 @@
content = '#python\n #好物物推薦' # 發文文章內容
postURL = 'https://www.facebook.com/marketingliveincode/posts/1590865937778957' #所分享的文章

# 設定基本參數
desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
#此處必須換成自己電腦的User-Agent
desired_capabilities['phantomjs.page.customHeaders.User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'

# PhantomJS driver 路徑 似乎只能絕對路徑
driver = webdriver.PhantomJS(executable_path = 'phantomjs', desired_capabilities=desired_capabilities)
# 自動下載ChromeDriver
service = ChromeService(executable_path=ChromeDriverManager().install())

# 關閉通知提醒
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)

# 開啟瀏覽器
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
driver = webdriver.Chrome(service=service, chrome_options=chrome_options)
time.sleep(5)

driver.get("http://www.facebook.com")
time.sleep(3)
assert "Facebook" in driver.title

driver.find_element_by_id('email').send_keys(useEmail) # 輸入帳號
driver.find_element(by=By.ID, value='email').send_keys(useEmail) # 輸入帳號
time.sleep(3)
driver.find_element_by_id('pass').send_keys(usePass) # 輸入密碼
driver.find_element(by=By.ID, value='pass').send_keys(usePass) # 輸入密碼
time.sleep(1)
driver.find_element_by_name('login').click() # 按下登入
driver.find_element(by=By.name, value='login').click() # 按下登入

# 發文準備
def goto_post(keyword):
driver.get(postURL)# 切換到想發的文章
time.sleep(10)

# 點選分享社團
driver.find_element_by_xpath('//div[@aria-label="寄送這個給朋友或張貼在你的動態時報中。"]').click()
driver.find_element(by=By.XPATH, value='//div[@aria-label="寄送這個給朋友或張貼在你的動態時報中。"]').click()
time.sleep(3)
driver.find_element_by_xpath("//*[contains(text(), '分享到社團')]").click()
driver.find_element(by=By.XPATH, value="//*[contains(text(), '分享到社團')]").click()
time.sleep(3)

# 切換分享身分
driver.find_element_by_xpath('//label[@aria-label="使用以下身分分享"]').send_keys('\ue015')
driver.find_element(by=By.XPATH, value='//label[@aria-label="使用以下身分分享"]').send_keys('\ue015')
time.sleep(5)
get_all_identity = driver.find_elements_by_xpath('//div[@aria-checked="false"]')
get_all_identity = driver.find_elements(by=By.XPATH, value='//div[@aria-checked="false"]')
for getid in get_all_identity:
if getid.text == identity:
getid.click()
break;
time.sleep(5)

# 將Keyword放入,搜尋社團
driver.find_element_by_xpath('//input[@aria-label="搜尋社團"]').send_keys(keyword)
driver.find_element(by=By.XPATH, value='//input[@aria-label="搜尋社團"]').send_keys(keyword)
time.sleep(3)


Expand All @@ -78,15 +77,15 @@ def goto_post(keyword):
goto_post(keyword) # 執行發文準備

# 計算該關鍵字共有幾個社團
count = len(driver.find_elements_by_xpath('//div[@style="padding-left: 8px; padding-right: 8px;"]/div[@role="button"]'))
count = len(driver.find_elements(by=By.XPATH, value='//div[@style="padding-left: 8px; padding-right: 8px;"]/div[@role="button"]'))
time.sleep(random.randint(5,10))

temp = 0 # 用來記錄已發文的社團
for long in range(count):
if long != 0: # 若不是第一次執行,則需要再次執行發文準備,因為發過文後「分享」按鈕會消失
goto_post(keyword)

getdriver = driver.find_elements_by_xpath('//div[@style="padding-left: 8px; padding-right: 8px;"]/div[@role="button"]')[temp]
getdriver = driver.find_elements(by=By.XPATH, value='//div[@style="padding-left: 8px; padding-right: 8px;"]/div[@role="button"]')[temp]
if len(getdriver.text) == 0: # 檢查
print('抓到空物件,下一個')
else:
Expand All @@ -96,13 +95,13 @@ def goto_post(keyword):
# 發文
time.sleep(random.randint(3,5))
try:
driver.find_element_by_xpath('//div[@aria-label="留個言吧......"]').send_keys(content)
driver.find_element(by=By.XPATH, value='//div[@aria-label="留個言吧......"]').send_keys(content)
except:
driver.find_element_by_xpath('//div[@aria-label="建立公開貼文……"]').send_keys(content)
driver.find_element(by=By.XPATH, value='//div[@aria-label="建立公開貼文……"]').send_keys(content)
time.sleep(random.randint(3,5))
driver.find_element_by_xpath('//input[@aria-checked="false"]').click()
driver.find_element(by=By.XPATH, value='//input[@aria-checked="false"]').click()
time.sleep(random.randint(2,5))
driver.find_element_by_xpath('//div[@aria-label="發佈"]').click()
driver.find_element(by=By.XPATH, value='//div[@aria-label="發佈"]').click()
time.sleep(random.randint(6,12))

temp = temp + 1 # 記錄已發文的社團
Expand Down

0 comments on commit 5b9eb75

Please sign in to comment.