我已经使用chrome驱动程序在selenium中编写了代码,它的代码在某些天工作正常,有些日子它会出错。我的代码是:
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()
请原谅预定的空间。
所以当我运行它时会收到此错误..
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
请,如果有人可以建议我改变什么,因为它昨天工作正常而不是它不起作用..它需要镀铬驱动程序才能运行
这是网站:https://maharerait.mahaonline.gov.in/searchlist/searchlist
答案 0 :(得分:1)
尝试使用select.select_by_value(districtElement)而不是index