0

enter image description hereenter image description herei have written a code in selenium using chrome driver, it codes works fine on some days and some days it gives error.Below is my code:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException


import os
import time
import csv

driver = webdriver.Chrome("chromedriver.exe")

driver.get('https://maharerait.mahaonline.gov.in/searchlist/searchlist')

# try:
#     element = WebDriverWait(driver, 100).until(
#         EC.presence_of_element_located((By.ID, "Promoter"))
#     )
# finally:
#     print('0000000000000000000000')
#     driver.quit()

time.sleep(1)

driver.find_element_by_id('Promoter').click()

divisionLength = len(Select(driver.find_element_by_id('Division')).options)
print('*********{}'.format(divisionLength))

firstRow = 0
titleRow = []
contentRows = []

gdistName = ""
gdivName  = ""

for divisionElement in range(1,divisionLength):
    selectDivision = Select(driver.find_element_by_id('Division'))
    selectDivision.options
    selectDivision.select_by_index(divisionElement)
    time.sleep(1)
    districtLength = 
len(Select(driver.find_element_by_id('District')).options)
    gdivName =  (selectDivision.options)[divisionElement].text
    while districtLength == 1:
        print("43")
    print(districtLength)
    for districtElement in range(1,districtLength):
        selectDistrict = Select(driver.find_element_by_id('District'))
        selectDistrict.options

        selectDistrict.select_by_index(districtElement)
        gdistName =  (selectDistrict.options)[districtElement].text

        time.sleep(2)
        projectLength = 
len(Select(driver.find_element_by_id('Project')).options)

        print('/------------------------------/')
        print('/-----project number: {}-------/'.format(projectLength))
        print('/------------------------------/')
        if projectLength == 1:
            continue

        for projectElement in range(1,projectLength):
            selectDistrict = Select(driver.find_element_by_id('District'))
            selectDistrict.select_by_index(0)
            selectDistrict.select_by_index(districtElement)
            time.sleep(2)
            gdistName =  (selectDistrict.options)[districtElement].text
            # selectProject.options

            # while len(selectProject.options) == 1:
            #     print(len(selectProject.options))
            #     print("65")
            # c = len(select.options)
            # print('---------------{}'.format(c))

            # titleRow = []
            # contentRows = []
            # firstRow = 0

            # for i in range(1,c):
            #     select = Select(driver.find_element_by_id('Project'))
            #     while len(select.options) == 1:
            #         pass
            selectProject = Select(driver.find_element_by_id('Project'))
            time.sleep(2)
            selectProject.select_by_index(projectElement)

            driver.find_element_by_id('btnSearch').click()
            tableRows = 
driver.find_element_by_class_name('table').find_elements_by_tag_name('tr')

            if firstRow == 0:
                headRow = tableRows[0].find_elements_by_tag_name('th')
                for headRowData in range(0,len(headRow)):
                    text = 
headRow[headRowData].find_element_by_tag_name('span').text
                    titleRow.append(text)
                firstRow = firstRow + 1

            for dataRowsNumbers in range(1,len(tableRows)):
                dataRow = 
tableRows[dataRowsNumbers].find_elements_by_tag_name('td')
                tempList = []
                for dataRowContents in range(0,len(dataRow)):
                    try:
                        a_link = 
dataRow[dataRowContents].find_element_by_tag_name('a').get_attribute('href')
                        tempList.append(str(a_link))
                    except NoSuchElementException:

                        tempList.append(str(dataRow[dataRowContents].text))
                    # if dataRow[dataRowContents].text == 'View':
                    #     a_link = 
dataRow[dataRowContents].find_element_by_tag_name('a').get_attribute('href')
                    #     tempList.append(str(a_link))
                    # else:
                    #     
tempList.append(str(dataRow[dataRowContents].text))
                    #print(dataRow[dataRowContents].text)
                tempList.append(gdivName)
                tempList.append(gdistName)
                print(tempList)
                contentRows.append(tempList)
# print('Automated check is over')
# print('Stored data in programs is as below:')
# print(contentRows)
with open("./data.csv",'w') as csvfile:
    csvfile = csv.writer(csvfile, delimiter=',')
    csvfile.writerow(titleRow)
    csvfile.writerow("")
    for i in range(0,len(contentRows)):
        csvfile.writerow(contentRows[i])

driver.close()

Please excuse of intended spaces.

so i receive this error when i run it..

Traceback (most recent call last):
  File "C:\Users\prince.bhatia\Desktop\Crawlers\Maha_Rera1.py", line 68, in 
<module>
    selectDistrict.select_by_index(districtElement)
  File 
"C:\Users\prince.bhatia\AppData\Local\Programs\Python\Python36\lib\site-
packages\selenium\webdriver\support\select.py", line 103, in select_by_index
    raise NoSuchElementException("Could not locate element with index %d" % 
index)
selenium.common.exceptions.NoSuchElementException: Message: Could not locate 
element with index 2

Please , if someone can suggest me what to change , because it worked fine yesterday and not it is not working..It requires chrome driver to run

this is the website: https://maharerait.mahaonline.gov.in/searchlist/searchlist

1 Answer 1

1

try using select.select_by_value(districtElement) instead of index

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

1 Comment

convert to string select.select_by_value(str(districtElement))

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.