1. 程式人生 > 實用技巧 >筆記記錄---等待元素

筆記記錄---等待元素

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# WPS表單的網址
url = 'https://f.wps.cn/form-write/uwDUPB2N/'
# 完成瀏覽器物件的初始化,設定超時時間為10秒。
browser = webdriver.Chrome()
wait 
= WebDriverWait(browser, 10) browser.get(url) ################################ # 針對INPUT元件,XXX替換成自己的內容。 answer = wait.until(EC.element_to_be_clickable((By.ID, 'input_0'))) answer.send_keys('XXX') # 針對LABEL元件。 answer = wait.until(EC.element_to_be_clickable((By.ID, 'select_label_wrap_1_0'))) answer.click()
# 針對INPUT元件,XXX替換成自己的內容。 answer = wait.until(EC.element_to_be_clickable((By.ID, 'input_2'))) answer.send_keys('18') # 針對LABEL元件。 answer = wait.until(EC.element_to_be_clickable((By.ID, 'select_label_wrap_3_0'))) answer.click() # 針對PICKER元件。 answer = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '
.ant-calendar-picker'))) answer.click() answer = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.ant-calendar-today-btn'))) answer.click() # 針對詢問時間的INPUT元件。 localtime = time.localtime(time.time()) if localtime.tm_hour < 7: t = 0 print("填寫時間為:0700-0900") elif localtime.tm_hour < 11: t = 1 print("填寫時間為:1100-1200") else: t = 2 print("填寫時間為:1800-2000") answer = wait.until(EC.element_to_be_clickable((By.ID, 'select_label_wrap_5_' + str(t)))) answer.click() ################################ # 等待10秒 time.sleep(10) # 點選提交 commit = wait.until(EC.element_to_be_clickable((By.ID, 'submit_button'))) commit.click() # 確認提交 yes = wait.until(EC.element_to_be_clickable((By.ID, 'bind_phone_modal_confirm_button'))) yes.click() # 反饋成功 print('Perfect!')