Timeout exception for selenium

219 views Asked by At

I'm trying to scrape the data from bscscan. My code is presented below.

I keep getting timeout exception. However after removing this line:

driver.find_element_by_xpath('/html/body/div[4]/table/thead/tr/th[2]/a').click()

the problem doesn't occur anymore. Does anyone have any idea how to solve this? Please note that I need to click the AGE button before exporting the data into the csv.

from selenium import webdriver
import pandas as pd
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

PATH = 'C:/Users/XX/Downloads/chromedriver_win32/chromedriver.exe'
driver = webdriver.Chrome(PATH)

driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://bscscan.com/token/0x20de22029ab63cf9a7cf5feb2b737ca1ee4c82a6#tokenTrade')
print(driver.title)
wait = WebDriverWait(driver, 20)
try:
  wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='btnCookie']"))).click()
except:
  pass
Txn_Hash = []
Age = []
Maker = []
Taker = []
Price = []
DEX = []
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "dextrackeriframe")))
#wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Age"))).click()
driver.find_element_by_xpath('/html/body/div[4]/table/thead/tr/th[2]/a').click()
table_size = len(driver.find_elements(By.XPATH, "//thead[@class='thead-light']/following-sibling::tbody//tr"))
print(table_size)
j = 1
for i in range(table_size):
    tnx_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/descendant::a[1]"))).text
    age_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[3]"))).text
    maker_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[4]"))).text
    Taker_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[6]"))).text
    Price_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[7]"))).text
    DEX_href = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[8]/img"))).get_attribute('src')
    Txn_Hash.append(tnx_text)
    Age.append(age_text)
    Maker.append(maker_text)
    Taker.append(Taker_text)
    Price.append(Price_text)
    DEX.append(DEX_href)
    j = j + 1

print(Txn_Hash)
print(Age)
print(Maker)
print(Taker)
print(Price)
print(DEX)

data = {
     'Transaction_HashKey': Txn_Hash,
     'Age': Age,
     'Maker': Maker,
     'Taker' : Taker,
     'Price' :Price,
     'DEX' : DEX
    }
df = pd.DataFrame.from_dict(data)
df.to_csv('output.csv', index = 0)
1

There are 1 answers

0
fam On

You are getting this error because the following line of code is not returning anything.

wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[3]")))

This line of code is valid, if you don't click the AGE button, but after performing the click action, nothing is returned. Also, you are missing the span tag.

Replace the above line of code with the following:

age_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[2]/span"))).text