close

上次的程式碼長這樣

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()

那實際執行會發現

未命名.png

有的時數他紋風不動....

那是因為在閱讀的時候, 還必需點擊一下課程內容

未命名.png

也就是點擊上圖不是環境檢測的其他欄位

但是不論我怎麼定位元素, 這次就是不給我用....

只好再借助外力了~這次用pyautogui

當然, 還是要先pip install pyautogui

接著試看看

import pyautogui

pyautogui.position()

擷取.PNG

這行函式的意思就是顯示目前滑鼠位置, 於是我們就有工具可以測量一下我們需要點擊的位置

經過我的檢測, 大概都落於300,255左右

pyautogui.moveTo(300,255,duration=0.25)

未命名.png

可以看到滑鼠在0.25秒的時間內移到300,255的位置上

pyautogui.click()

在加上這段滑鼠就可以點擊

完整程式碼 :

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()
    driver.implicitly_wait(10)

    import pyautogui
    pyautogui.moveTo(300,255,duration=0.25)
    sleep(1)
    pyautogui.click()


    sleep(900)
    driver.quit()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

arrow
arrow
    全站熱搜

    張郎屋 發表在 痞客邦 留言(0) 人氣()