python selenium-3

標籤ID
driver.find_element(By.ID,"條件")

標籤屬性搜尋 CSS
driver.find_element(By.CSS_SELECTOR,"[屬性名稱=屬性的值]")

模擬user input
element=driver.find_elemnet(search欄位,"條件")
element.send_keys("input key)

模擬 user inpurt key
element.send_keys(Keys.ENTER)
=======================================
#https://www.youtube.com/watch?v=tR558fQAHz8
#爬 leetcode
#https://leetcode.com/accounts/login/
#pip install selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time #wating html loading

#chrom driver path https://chromedriver.chromium.org/downloads
options=Options()
options.chrome_executable_path="G:\我的雲端硬碟\PY\練習\chromdriver"

# Driver 物件
driver=webdriver.Chrome(options=options)

#連線到 LeetCode
driver.get("https://leetcode.com/accounts/login/")

#輸入帳號密碼, 按下 Enter
usernameInput=driver.find_element(By.ID, "id_login")
passwordInput=driver.find_element(By.ID, "id_password")

usernameInput.send_keys("myid")
passwordInput.send_keys("mypassword")

siginBtn=driver.find_element(By.ID, "signin_btn")
siginBtn.send_keys(Keys.ENTER)

#wait loading
time.sleep(5)

#連線到登入後才能取得Data
driver.get("https://leetcode.com/problemset/all/")
time.sleep(3)
statElement=driver.find_element(By.CSS_SELECTOR,"[data-difficulty=TOTAL]")
#print(statElement.text)
columns=statElement.text.split("\n") # List
print("已完成刷題數量: " + columns[1])

driver.close()

本篇發表於 python。將永久鍊結加入書籤。

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>