首页 > 解决方案 > 使用 selenium 抓取超时异常错误

问题描述

我正在尝试根据此站点https://www.bi.go.id/id/statistik/informasi-kurs/transaksi-bi/Default.aspx使用此代码的日期来抓取表格

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import re
import time
from bs4 import BeautifulSoup
# Import webdriver to initialise a browser
from selenium.webdriver import ActionChains
import csv
import pandas as pd
import requests
driver = webdriver.Chrome()
driver.get("https://www.bi.go.id/id/statistik/informasi-kurs/transaksi-bi/Default.aspx")

wait = WebDriverWait(driver, 10)

# click "usd"



book = wait.until(
    EC.element_to_be_clickable((By.XPATH,' //*[@id="selectPeriod"]/option[range]'))
)
book.click()

book1 = wait.until(
    EC.element_to_be_clickable((By.XPATH,' //*[@id="ctl00_PlaceHolderMain_g_6c89d4ad_107f_437d_bd54_8fda17b556bf_ctl00_ddlmatauang1"]/option[USD]'))
)
book1.click()



start_date = driver.find_element_by_id("ctl00_PlaceHolderMain_g_6c89d4ad_107f_437d_bd54_8fda17b556bf_ctl00_txtFrom")
start_date.send_keys("20-Nov-15")
end_date = driver.find_element_by_id("ctl00_PlaceHolderMain_g_6c89d4ad_107f_437d_bd54_8fda17b556bf_ctl00_txtTo")
end_date.send_keys("20-Nov-20")
submit_button = driver.find_element_by_id("ctl00_PlaceHolderMain_g_6c89d4ad_107f_437d_bd54_8fda17b556bf_ctl00_btnSearch1").click()

但是我得到了这个错误,我不知道为什么。有人可以帮我解决问题吗?

-------------------------------------------------- ------------------------- TimeoutException Traceback (最近一次调用最后一次) in 9 10 ---> 11 book = wait.until( 12 EC. element_to_be_clickable((By.XPATH,' //*[@id="selectPeriod"]/option[range]')) 13 )

~\anaconda3\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message) 78 if time.time() > end_time: 79 break ---> 80 raise TimeoutException(message,屏幕,堆栈跟踪)81 82 def until_not(自我,方法,消息=''):

超时异常:消息:

标签: pythonseleniumscreen-scraping

解决方案


这应该可以工作,因为它是一个 Select 标签。

driver.get("https://www.bi.go.id/id/statistik/informasi-kurs/transaksi-bi/Default.aspx")
wait = WebDriverWait(driver, 10)
book = wait.until(EC.element_to_be_clickable((By.ID,"selectPeriod")))
sel = Select(book)
sel.select_by_value("range")

进口

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

推荐阅读