那麼開始今天的複雜的~醜醜的程式碼
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome(r"C:\selenium_driver_chrome\chromedriver.exe")
首先是起手式, 載入套件們, 還有chromedriver
driver.implicitly_wait(10)
driver.maximize_window()
然後是"隱性等待", 意思是瀏覽器如果還沒開好, 等他10秒, 如果已經開好就不用等
可以看這位大大寫的很清楚喔! https://huilansame.github.io/huilansame.github.io/archivers/sleep-implicitlywait-wait
接著我喜歡放大視窗
driver.get("https://www.cp.gov.tw/portal/Clogin.aspx?ReturnUrl=https%3A%2F%2Felearn.hrd.gov.tw%2Fegov_login.php")
sleep(1)
接著打開今天的女主角(為啥是女生?)這個網頁!!!!
driver.find_element_by_xpath("//input[@name='ctl00$ContentPlaceHolder1$AccountPassword1$txt_account']").send_keys("你的帳號")
sleep(1)
那老樣子, 我們用檢查元素or鍵盤F12打開網頁~
為了定位到我們要的帳號這一個輸入框, 用上圖打圈圈的箭頭工具, 點一下可以看到反藍的網頁原始碼位置
然後輸入我的帳號, 你當然要輸你的囉!!!
driver.find_element_by_xpath("//input[@name='ctl00$ContentPlaceHolder1$AccountPassword1$txt_password']").click()
其實不應該多上面這行的, 但不知道怎麼她一直不肯給我填入下一格的密碼, 定位的方式也是一樣的~~
driver.find_element_by_xpath("//input[@name='ctl00$ContentPlaceHolder1$AccountPassword1$txt_password']").send_keys("你的密碼")
sleep(1)
不用說, 這段程式碼你也可以了解吧!附帶一提, sleep()是強制等待~
可以得到我們要的結果, 如上圖的示意圖
driver.find_element_by_xpath("//input[@name='ctl00$ContentPlaceHolder1$AccountPassword1$btn_LoginHandler']").click()
sleep(2)
接著要按登入按鈕
放大一點讓大家看清楚, 我是找他的name
driver.get("https://elearn.hrd.gov.tw/mooc/user/registration_center.php#")
sleep(3)
接著是轉換網頁, 因為她直接登入的網頁很麻煩要轉來轉去, 直接進這個網頁比較方便
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[8]/div/div/div/button").click()
然後我想要動選課狀態這個欄位, 本來以為是下拉式選單, 我導入Select函式選他, 但Selenium告訴我這只是個button
這次改用滑鼠右鍵, 選擇copy -> Copy full XPath, 可以得到
/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[8]/div/div/div/button
直接拿來用就可以了~
可以看到可愛的下拉式選單被弄開了~
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[8]/div/div/div/ul/li[4]/a").click()
#driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[10]/div/div/div/label[2]/input").click()
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[10]/div/div/div/label[1]/input").click()
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[12]/div/div/button").click()
效果如上, 為得是得到尚未通過的第一筆課程, 畫面上就是疫情報導~~~
driver.find_element_by_xpath('//*[@alt="課程代表圖"]').click()
那這次檢查元素我用的是alt="課程代表圖", 夠簡單吧!!
driver.switch_to.window(driver.window_handles[1])
那這次可以看到他開成兩個分頁, 所以要換window_handles
不然下一個指令不能繼續~
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[4]/div/div[1]/div[2]/div/div[3]/div[2]/button").click()
這段是點擊上課去
sleep(900)
等15分鐘~
driver.quit()
然後關閉瀏覽器~~
那基本30分鐘起跳, 所以我迴圈兩次到三次, 完整程式碼如下 :
from selenium import webdriver
from time import sleep
for i in range(3):
driver = webdriver.Chrome(r"C:\selenium_driver_chrome\chromedriver.exe")
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("https://www.cp.gov.tw/portal/Clogin.aspx?ReturnUrl=https%3A%2F%2Felearn.hrd.gov.tw%2Fegov_login.php")
sleep(1)
driver.find_element_by_xpath("//input[@name='ctl00$ContentPlaceHolder1$AccountPassword1$txt_account']").send_keys("你的帳號")
sleep(1)
driver.find_element_by_xpath("//input[@name='ctl00$ContentPlaceHolder1$AccountPassword1$txt_password']").click()
driver.find_element_by_xpath("//input[@name='ctl00$ContentPlaceHolder1$AccountPassword1$txt_password']").send_keys("你的密碼")
sleep(1)
driver.find_element_by_xpath("//input[@name='ctl00$ContentPlaceHolder1$AccountPassword1$btn_LoginHandler']").click()
sleep(2)
driver.get("https://elearn.hrd.gov.tw/mooc/user/registration_center.php#")
sleep(3)
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[8]/div/div/div/button").click()
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[8]/div/div/div/ul/li[4]/a").click()
#driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[10]/div/div/div/label[2]/input").click()
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[10]/div/div/div/label[1]/input").click()
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div/div/div[2]/form/div/div[12]/div/div/button").click()
driver.find_element_by_xpath('//*[@alt="課程代表圖"]').click()
driver.switch_to.window(driver.window_handles[1]) #切換分頁
driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[4]/div/div[1]/div[2]/div/div[3]/div[2]/button").click()
sleep(900)
driver.quit()