下面的脚本将转到该页面并下载上市公司的相关财务报表。
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)
该脚本在步骤上有两个错误:
,都具有相同的错误消息:
TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given
发布于 2022-11-01 08:00:57
而不是
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()
相应地。
有关更多解释,请参见this answer。
另外,你必须改进你的定位器。长的绝对XPaths是非常脆弱和不可靠的。
另外,这里
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://stackoverflow.com/questions/74270599
复制相似问题