這是我自己寫一個買網路商品的python(初學者)
邊學邊寫,出錯查錯
但好奇怪方法都很單純,前面寫的很順,後面卻變成錯誤...
操作的步驟也都是跟前面一樣的方法
import selenium.webdriver.support.ui as ui
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
PATH = "ChromeDriver檔案位子"
driver = webdriver.Chrome(PATH)
driver.get("購物網址")
link = driver.find_element(By.CLASS_NAME,"buy-now-text")
link.click()
time.sleep(3)
email = driver.find_element(By.XPATH,'/html/body/div[8]/div[1]/div/div/div/div/div/div/div/div/div[2]/div/div/div/form/div[1]/div/div/input')#輸入帳號
email.send_keys('帳號')
tgg = driver.find_element(By.XPATH,"/html/body/div[8]/div[1]/div/div/div/div/div/div/div/div/div[2]/div/div/div/form/div[2]/div/div/input")#輸入密碼
tgg.send_keys("密碼")
tgc = driver.find_element(By.XPATH,'//*[@id="sign-in-recaptcha"]')#點擊登入
tgc.click()
time.sleep(15)
tgb = driver.find_element(By.LINK_TEXT,'前往結帳')#點擊買單
tgb.click()
time.sleep(3)
tge = driver.find_element(By.ID,'recipient-name')#名子欄位
tge.send_keys("名子")
tgd = driver.find_element(By.ID,'recipient-phone')#電話欄位
tgd.send_keys("電話")
tgf = driver.find_element(By.XPATH,'//*[@id="seven-eleven-address"]/div/div')#進入7-11電子地圖查詢系統
tgf.click()
time.sleep(2)
tgh = driver.find_element(By.XPATH,'//*[@id="byID"]/a')#點擊門市店號
tgh.click()
time.sleep(2)
tgi = driver.find_element(By.ID,"storeIDKey")#我要在這欄位打字
tgi.send_keys("148948")#打這些數字
最後這串tgi我試了CLASS_NAME,XPATH,ID,都不行會跑出一堆錯誤...
tgi = driver.find_element(By.ID,"storeIDKey")
tgi.send_keys("148948")
錯誤訊息如下
Stacktrace:
Backtrace:
Ordinal0 [0x0023ACD3+2075859]
Ordinal0 [0x001CEE61+1633889]
Ordinal0 [0x000CB7BD+571325]
Ordinal0 [0x000FAC2F+764975]
Ordinal0 [0x000FAE1B+765467]
Ordinal0 [0x0012D0F2+970994]
Ordinal0 [0x00117364+881508]
Ordinal0 [0x0012B56A+963946]
Ordinal0 [0x00117136+880950]
Ordinal0 [0x000EFEFD+720637]
Ordinal0 [0x000F0F3F+724799]
GetHandleVerifier [0x004EEED2+2769538]
GetHandleVerifier [0x004E0D95+2711877]
GetHandleVerifier [0x002CA03A+521194]
GetHandleVerifier [0x002C8DA0+516432]
Ordinal0 [0x001D682C+1665068]
Ordinal0 [0x001DB128+1683752]
Ordinal0 [0x001DB215+1683989]
Ordinal0 [0x001E6484+1729668]
BaseThreadInitThunk [0x76E3FEF9+25]
RtlGetAppContainerNamedObjectPath [0x774B7BBE+286]
RtlGetAppContainerNamedObjectPath [0x774B7B8E+238]
(No symbol) [0x00000000]
Process finished with exit code 1
然後最後我發現,同樣程序本來可以執行(就是這串前面輸入帳號、密碼的時候)
什麼東西都沒改,之後竟然會莫名其妙出錯...不能執行
稍微去重抓XPATH(好像沒變) 之後又可以了...
有大師能指點一下嗎...
認真在學
錯在門市輸入那串
改成這樣試試?
我記得地圖查詢系統都沒有那麼快
兩秒可能不太夠
還沒載入好就急著想點門市店號(猜測)
下面這方法是等待10秒
10秒內有載入就會繼續執行
超過10秒跳TimeOut錯誤
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
PATH = "ChromeDriver檔案位子"
driver = webdriver.Chrome(PATH)
driver.get("購物網址")
# 點擊進入結帳頁面的按鈕
link = driver.find_element(By.CLASS_NAME, "buy-now-text")
link.click()
# 輸入帳號和密碼等登入信息
email = driver.find_element(By.XPATH, '/html/body/div[8]/div[1]/div/div/div/div/div/div/div/div/div[2]/div/div/div/form/div[1]/div/div/input')
email.send_keys('帳號')
password = driver.find_element(By.XPATH, "/html/body/div[8]/div[1]/div/div/div/div/div/div/div/div/div[2]/div/div/div/form/div[2]/div/div/input")
password.send_keys("密碼")
login_button = driver.find_element(By.XPATH, '//*[@id="sign-in-recaptcha"]')
login_button.click()
# 等待結帳按鈕可見,然後點擊結帳
checkout_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.LINK_TEXT, "前往結帳")))
checkout_button.click()
# 輸入收件人姓名和電話號碼
recipient_name = driver.find_element(By.ID, 'recipient-name')
recipient_name.send_keys("名子")
recipient_phone = driver.find_element(By.ID, 'recipient-phone')
recipient_phone.send_keys("電話")
# 進入7-11電子地圖查詢系統
eleven_map_button = driver.find_element(By.XPATH, '//*[@id="seven-eleven-address"]/div/div')
eleven_map_button.click()
# 點擊門市店號鏈接
store_id_link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "byID")))
store_id_link.click()
# 等待並輸入門市店號
store_id_input = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "storeIDKey")))
store_id_input.send_keys("148948")