下面的腳本旨在進入該頁面并下載上市公司的相關財務報表。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import numpy as np
options = Options()
options.add_argument("--start-maximized")
import time
start = time.process_time()
time.sleep(3)
s = Service(path)
driver = webdriver.Chrome(options=options, service=s)
#go to page
page = 'https://www.idx.co.id/perusahaan-tercatat/laporan-keuangan-dan-tahunan/'
driver.get(page)
from selenium.webdriver.common.keys import Keys
try:
#click on the input button
wait = WebDriverWait(driver, 2)
inputElement = wait.until(EC.element_to_be_clickable((By.XPATH,
"/html/body/main/div[1]/div[2]/div[3]/div/span/span[1]/span/ul/li/input")))
inputElement.send_keys(company, Keys.ENTER)
#input Element 2 - choose year,2022
inputElement2 = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yearList"]/option[1]'))).click()
#choose period
inputElement3 = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,
"#periodList > option:nth-child(3)"))).click()
#click on "Cari" button
wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR,"#searchButton")).click()
#returns TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given
except:
pass
#download the file
wait.until(EC.element_to_be_clickable(By.XPATH,
"/html/body/main/div[2]/div/div/div[2]/div/dl/dd[5]/div[1]/a/text()")).click()
#returns TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given
print('Execution Time: ', time.process_time() - start)
該腳本在步驟上有 2 個錯誤:
- 點擊“cari”按鈕
- 并下載檔案
,兩者都有相同的錯誤訊息:
TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given
uj5u.com熱心網友回復:
代替
wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR, "#searchButton")).click()
和
wait.until(EC.element_to_be_clickable(By.XPATH, "/html/body/main/div[2]/div/div/div[2]/div/dl/dd[5]/div[1]/a/text()")).click()
它應該是
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#searchButton"))).click()
和
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/main/div[2]/div/div/div[2]/div/dl/dd[5]/div[1]/a/text()"))).click()
相應地。
請參閱此答案以獲取更多解釋。
此外,您必須改進定位器。長絕對 XPath 非常脆弱且不可靠。
此外,這里
inputElement2 = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yearList"]/option[1]'))).click()
和這里
inputElement3 = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#periodList > option:nth-child(3)"))).click()
您不需要宣告inputElement2和inputElement3變數,因為web_element.click()方法不回傳任何內容,因此這些變數正在獲取/保留NoneType物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/524616.html
