Home > database >  TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given [Selenium | Python
TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given [Selenium | Python

Time:11-01

The script below is intended to go to the page and download relevant financial statements of publicly-traded companies.

    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)

The script has 2 errors on the steps:

  1. click on "cari" button
  2. and download the file

, both with the same error message:

TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given

CodePudding user response:

Instead of

wait.until(EC.element_to_be_clickable(By.CSS_SELECTOR, "#searchButton")).click()

and

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()

It should be

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#searchButton"))).click()

and

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()

correspondingly.
Please see this answer for more explanations.
Also, you have to improve your locators. Long absolute XPaths are very fragile and not reliable.
Additionally, here

inputElement2 = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yearList"]/option[1]'))).click()

and here

inputElement3 = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#periodList > option:nth-child(3)"))).click()

You do not need to declare inputElement2 and inputElement3 variables since web_element.click() method returns nothing, so these variables are getting / staying a NoneType objects.

  • Related