0

I am trying to get the data from this web page https://rapor.epias.com.tr/rapor/xhtml/ptfSmfListeleme.xhtml As you can see I need to pick a beginning and end date and get the data. But the problem is when u open the calendar you can see the non-active days too especially at the final date.

I write a code for the beginning date and it is worked well but it is not practical.

import re
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url = r'https://rapor.epias.com.tr/rapor/xhtml/ptfSmfListeleme.xhtml'

driver = webdriver.Chrome(r'...\chromedriver.exe')
driver.get(url)
link = driver.find_element_by_xpath('//*[@id="PmumBI:baslangicTarih"]/button/span[1]')
link.click()
while True:
    try:
        input_date = driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[1]/td[1]/a')
        input_date.click()
    except:
        try:
            input_date = driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[1]/td[2]/a')
            input_date.click()
        except:
            try:
                input_date = driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[1]/td[3]/a')
                input_date.click()
            except:
                try:
                    input_date = driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[1]/td[4]/a')
                    input_date.click()
                except:
                    try:
                        input_date = driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[1]/td[5]/a')
                        input_date.click()
                    except:
                        try:
                            input_date = driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[1]/td[6]/a')
                            input_date.click()
                        except:
                            try:
                                input_date = driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[1]/td[7]/a')
                                input_date.click()
                            except:
                                break

But I if I want to use the same for the final date I have to create more then 7 try code. Is there any way to make this simple, Like choosing between the active dates?

1
  • Could you give an example for a startDate and an endDate which are inactive? As I click through the calendar they all seem selectable? Also, do you have a range of date you need to go through or do you just need random data from the site? Commented Dec 22, 2020 at 15:15

1 Answer 1

2

Instead of :

while True:

You can use a for loop and use the index inside your xpath expression :

for i in range(20):
    try:
        input_date = driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[1]/td[' +str(i)+']/a')
        input_date.click()
        break
    except:
        pass

Hope it'll help !

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it is, thanks a lot somehow I couldn't think [' +str(i)+'] like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.