Selenium in python is giving error -
i have written code in selenium using chrome driver, codes works fine on days , days gives error.below code:
from selenium import webdriver selenium.webdriver.support.ui import select selenium.webdriver.common.by import selenium.webdriver.support.ui import webdriverwait selenium.webdriver.support import expected_conditions ec 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 = "" 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) 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 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 # 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') headrowdata in range(0,len(headrow)): text = headrow[headrowdata].find_element_by_tag_name('span').text titlerow.append(text) firstrow = firstrow + 1 datarowsnumbers in range(1,len(tablerows)): datarow = tablerows[datarowsnumbers].find_elements_by_tag_name('td') templist = [] 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 over') # print('stored data in programs below:') # print(contentrows) open("./data.csv",'w') csvfile: csvfile = csv.writer(csvfile, delimiter=',') csvfile.writerow(titlerow) csvfile.writerow("") in range(0,len(contentrows)): csvfile.writerow(contentrows[i]) driver.close()
please excuse of intended spaces.
so receive error when 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 index %d" % index) selenium.common.exceptions.nosuchelementexception: message: not locate element index 2
please , if can suggest me change , because worked fine yesterday , not not working..it requires chrome driver run
this website: https://maharerait.mahaonline.gov.in/searchlist/searchlist
try using select.select_by_value(districtelement) instead of index
Comments
Post a Comment