3

After taking suggestions from people here, I think I reached somewhere. I again got stuck. I have to scrape data from a website using datepicker. html says that the website is using Zebra datapicker. I have to click on date and then scrape the data. I am struggling to click on date. Can someone help me?

I have to click on 1/06/19. I am not able to figure out a way to do this. For reference: http://apsdps.ap.gov.in/des_rainfall.html This is the website.

link = "http://apsdps.ap.gov.in/des_rainfall.html"
webdriver.Chrome(executable_path=r"chromedriver.exe")
driver = webdriver.Chrome() 
driver.get(link)
#driver.switch_to_frame(driver.find_element_by_xpath("//iframe"))
input_date = driver.find_element_by_name("date1")
input_date.click()

HTML:

<div class="Zebra_DatePicker" style="left: 222.375px; display: none; top: 0px;">
   <table class="dp_header" style="width: 218px;">
      <tbody>
         <tr>
            <td class="dp_previous">«</td>
            <td class="dp_caption">June, 2019</td>
            <td class="dp_next">»</td>
         </tr>
      </tbody>
   </table>
   <table class="dp_daypicker">
      <tbody>
         <tr>
            <th>Mo</th>
            <th>Tu</th>
            <th>We</th>
            <th>Th</th>
            <th>Fr</th>
            <th>Sa</th>
            <th>Su</th>
         </tr>
         <tr>
            <td class="dp_not_in_month">27</td>
            <td class="dp_not_in_month">28</td>
            <td class="dp_not_in_month">29</td>
            <td class="dp_not_in_month">30</td>
            <td class="dp_not_in_month">31</td>
            <td class="dp_weekend dp_selected">1</td>
            <td class="dp_weekend">2</td>
         </tr>
         <tr>
            <td>3</td>
            <td class="">4</td>
            <td>5</td>
            <td>6</td>
            <td>7</td>
            <td class="dp_weekend">8</td>
            <td class="dp_weekend">9</td>
         </tr>
         <tr>
            <td>10</td>
            <td>11</td>
            <td class="">12</td>
            <td>13</td>
            <td>14</td>
            <td class="dp_weekend">15</td>
            <td class="dp_weekend">16</td>
         </tr>
         <tr>
            <td>17</td>
            <td>18</td>
            <td class="">19</td>
            <td>20</td>
            <td>21</td>
            <td class="dp_weekend">22</td>
            <td class="dp_weekend">23</td>
         </tr>
         <tr>
            <td>24</td>
            <td>25</td>
            <td class="">26</td>
            <td>27</td>
            <td>28</td>
            <td class="dp_weekend">29</td>
            <td class="dp_weekend">30</td>
         </tr>
         <tr>
            <td class="dp_not_in_month">1</td>
            <td class="dp_not_in_month">2</td>
            <td class="dp_not_in_month">3</td>
            <td class="dp_not_in_month">4</td>
            <td class="dp_not_in_month">5</td>
            <td class="dp_not_in_month">6</td>
            <td class="dp_not_in_month">7</td>
         </tr>
      </tbody>
   </table>
   <table class="dp_monthpicker" style="width: 218px; height: 190px; display: none;"></table>
   <table class="dp_yearpicker" style="width: 218px; height: 190px; display: none;"></table>
   <table class="dp_footer" style="width: 218px;">
      <tbody>
         <tr>
            <td class="dp_today" style="width: 50%;">Today</td>
            <td class="dp_clear" style="width: 50%; display: table-cell;">Clear date</td>
         </tr>
      </tbody>
   </table>
</div>
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
G.S. J
  • 233
  • 1
  • 8

1 Answers1

1

To send a date to the datepicker you can use the following Locator Strategies:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("http://apsdps.ap.gov.in/des_rainfall.html")
    driver.execute_script("arguments[0].removeAttribute('readonly')", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#datepicker-example6"))))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#datepicker-example6"))).send_keys("07/06/2019")
    
  • Browser Snapshot:

date

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Using JS to alter elements like removing the readonly attribute is generally a bad practice because you are shortcutting UI flow determined by the site devs. It can cause all kinds of problems now or later, along with with not being a actual user flow. You should figure out how a user would interact with the site and then automate those steps. – JeffC Aug 20 '19 at 17:59
  • @JeffC It's still not clear why instead of asking a question you keep on commenting counter questions. I am sure your this counter question have been answered numerous times by different contributors. If it's still not clear to you feel free to raise a question. Stackoverflow contributors will be happy to help you out. – undetected Selenium Aug 20 '19 at 18:41
  • There's no question in my first comment. I was making the same point that I've made before on your and others' answers. This is a bad practice. Reread my first comment if you are unclear. – JeffC Aug 20 '19 at 19:24