Skip to content

Instantly share code, notes, and snippets.

@chingyuan1215
Created September 17, 2020 15:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chingyuan1215/abda4c95c04bc48d43651364245dfcbb to your computer and use it in GitHub Desktop.
Save chingyuan1215/abda4c95c04bc48d43651364245dfcbb to your computer and use it in GitHub Desktop.
# selenium
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup as Soup
from selenium.webdriver.common.action_chains import ActionChains
from pandas.core.frame import DataFrame
import time
import pandas as pd
options = webdriver.ChromeOptions()
prefs = {
'profile.default_content_setting_values':
{
'notifications': 2
}
}
options.add_experimental_option('prefs', prefs)
options.add_argument("disable-infobars")
# ------ 設定要前往的網址 ------
url = 'https://www.facebook.com'
# ------ 登入的帳號與密碼 ------
username = ''
password = ''
# ------ 透過Browser Driver 開啟 Chrome ------
driver = webdriver.Chrome('C:/Users/user/Downloads/chromedriver_win32/chromedriver.exe')
# ------ 前往該網址 ------
driver.get(url)
# ------ 賬號密碼 ------
# time.sleep(1)
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[@id="email"]')))
elem = driver.find_element_by_id("email")
elem.send_keys(username)
elem = driver.find_element_by_id("pass")
elem.send_keys(password)
elem.send_keys(Keys.RETURN)
time.sleep(5)
#檢查有沒有被擋下來
if len(driver.find_elements_by_xpath("//*[contains(text(), '你的帳號暫時被鎖住')]")) > 0:
driver.find_elements_by_xpath("//*[contains(text(), '是')]")[1].click()
# 切換頁面
spec_url = 'https://www.facebook.com/moea.gov.tw'
driver.get(spec_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment