0

I am trying to extract table from a website using python selenium. In the webpage, i need to select the desired date. For day and month there are two dropdown box. For these two i am using css selector and able to select the date and month. But the problem is with year. It is a text box with a default value '2020' the current year. So first I simply tried sending "2019" as input. But that wouldn't work and return the default page data, i.e, the current date data.

Then I tried clearing the textbox and then send the input as "2019". This method returns a table with NANs. How can I send 2019 as input for year?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from datetime import datetime, timedelta
from math import ceil
import pandas as pd
import numpy as np
from time import time, sleep
import re

CHROMEDRIVER_PATH = '/path /to/chromedriver'

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)

driver.get("URL")
# date = datetime.now() - timedelta(days=1)
date = datetime(2019,11,4)
year = date.strftime("%Y")
month = date.strftime("%m")
day = date.strftime("%d")
date = date.strftime("%Y-%m-%d")

driver.find_element_by_css_selector("#ContentPlaceHolder2_txtyear").clear() #Clear the default value from the box
sleep(1)
driver.find_element_by_css_selector("#ContentPlaceHolder2_txtyear").send_keys(year) #Send the year as a string
sleep(1)
driver.find_element_by_css_selector("#ContentPlaceHolder2_txtyear").send_keys(Keys.ENTER) #Hit enter after sending the key
sleep(1)

driver.find_element_by_css_selector("#ContentPlaceHolder2_ddmonth").send_keys(month) #Send input for month
driver.find_element_by_css_selector("#ContentPlaceHolder2_ddday").send_keys(day) # send input for day

html = driver.page_source

pd.read_html(html)[0]

Edit: The year textbox element from source code is as follows:

<input name="ctl00$ContentPlaceHolder2$txtyear"
       type="text"
       value="2020"
       onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder2$txtyear\',\'\')', 0)"
       onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;"
       id="ContentPlaceHolder2_txtyear"
       style="height:16px;width:46px;margin-left: 0px">
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92

1 Answers1

0

The relevant HTML of the desired element would have helped us to analyze the issue in a better way.

However, to locate and invoke click() / clear() / send_keys() on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#ContentPlaceHolder2_txtyear")))
    element.click()
    element.clear()
    element.send_keys("user11593389")
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='ContentPlaceHolder2_txtyear']")))
    element.click()
    element.clear()
    element.send_keys("user11593389")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Tried doing this earlier, but it threw an error at ```element.send_keys("2019")``` as follows ```StaleElementReferenceException: Message: stale element reference: element is not attached to the page document``` – user11593389 Feb 03 '20 at 11:35
  • @user11593389 Update the question with the relevant HTML. – undetected Selenium Feb 03 '20 at 12:05
  • @user11593389: Just clearing and sending the keys worked for me. Give it a try, element.clear() and then element.send_keys("user11593389"). Adding the click gave me an error too. – The AG May 27 '21 at 15:10