Please can anyone help me with this code: I am trying to iterate on each element in column and click on it, but it gives me error. "can not locate the element" but when I get rid of the for loop and try one element it works.
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="C:\Program Files (x86)\chromedriver.exe")
driver.get("http://www.dc.state.fl.us/OffenderSearch/list.aspx?TypeSearch=IR&Page=List&DataAction=Filter&dcnumber=&LastName=a&FirstName=&SearchAliases=1&OffenseCategory=&ZipCode=&ReleaseDateBeg=10/01/1997&ReleaseDateEnd=&CountyOfCommitment=&StatedCountyOfResidence=&ReleaseFacility=&photosonly=0&nophotos=1&matches=20")
driver.implicitly_wait(10)
for i in range(2,6):
person = driver.find_element(By.XPATH, '//table[@id="ctl00_ContentPlaceHolder1_GrdReleasesPublic"]/tbody/tr[i]/td[1]/a').click()
#person = driver.find_element(By.XPATH,"/html[1]/body[1]/div[5]/div[1]/div[1]/div[1]/form[1]/div[3]/div[1]/div[1]/div[3]/table[1]/tbody[1]/tr[row]/td[1]/a[1]").click()
driver.implicitly_wait(5)
# retriving info about the inmate
person_info = driver.find_element(By.CLASS_NAME, "offenderDetails").text
alias = driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_divAlias").text
al = alias.replace('\n', ' ')
y = person_info "\n" al
#print(y)
person_info.strip(',')
with open('readme.txt', 'w') as f:
f.write(y)
#print(person_info)
myfile = open("readme.txt", "r")
data_dic = {}
for line in myfile:
#print(line)
k, v = line.strip('').split(":")
data_dic[k.strip()] = v.strip()
myfile.close()
print(data_dic)
header = ['DC Number', 'Name', 'Race', 'Sex', 'Birth Date', 'Custody', 'Release Date', 'Aliases' ]
new_dic = [data_dic]
print(new_dic)
with open('test4.csv', 'w') as csvfile1:
writer = csv.DictWriter(csvfile1, fieldnames=header)
writer.writeheader()
writer.writerows(new_dic)
driver.get("http://www.dc.state.fl.us/OffenderSearch/list.aspx?TypeSearch=IR&Page=List&DataAction=Filter&dcnumber=&LastName=a&FirstName=&SearchAliases=1&OffenseCategory=&ZipCode=&ReleaseDateBeg=10/01/1997&ReleaseDateEnd=&CountyOfCommitment=&StatedCountyOfResidence=&ReleaseFacility=&photosonly=0&nophotos=1&matches=20")
driver.implicitly_wait(10)
CodePudding user response:
You don't need to click on each link on the table, rather you can capture all the href
value in a list and then iterate and navigate to each page.
Code:
driver.get("http://www.dc.state.fl.us/OffenderSearch/list.aspx?TypeSearch=IR&Page=List&DataAction=Filter&dcnumber=&LastName=a&FirstName=&SearchAliases=1&OffenseCategory=&ZipCode=&ReleaseDateBeg=10/01/1997&ReleaseDateEnd=&CountyOfCommitment=&StatedCountyOfResidence=&ReleaseFacility=&photosonly=0&nophotos=1&matches=20")
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@id='ctl00_ContentPlaceHolder1_GrdReleasesPublic']")))
#Get list of urls
urlList=[url.get_attribute('href') for url in driver.find_elements(By.XPATH,"//table[@id='ctl00_ContentPlaceHolder1_GrdReleasesPublic']//tbody//td[2]//a")]
for url in urlList:
driver.get(url)
#do what you wish
You need to import below libarries.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
CodePudding user response:
you can use row as parameter and construct xpath, and get each element's 'href', then new_tab to open the page, and get personal info and alias. It is really easy and straightforward to do that using clicknium:
from clicknium import clicknium as cc
if not cc.chrome.extension.is_installed():
cc.chrome.extension.install_or_update()
tab = cc.chrome.open("http://www.dc.state.fl.us/OffenderSearch/list.aspx?TypeSearch=IR&Page=List&DataAction=Filter&dcnumber=&LastName=a&FirstName=&SearchAliases=1&OffenseCategory=&ZipCode=&ReleaseDateBeg=10/01/1997&ReleaseDateEnd=&CountyOfCommitment=&StatedCountyOfResidence=&ReleaseFacility=&photosonly=0&nophotos=1&matches=20")
xpath_template = '//*[@id="ctl00_ContentPlaceHolder1_GrdReleasesPublic"]/tbody/tr[{}]/td[2]/a'
row = 2
while True:
xpath = xpath_template.format(row)
if tab.is_existing_by_xpath(xpath):
href = tab.find_element_by_xpath(xpath).get_property('href')
url = "http://www.dc.state.fl.us{}".format(href)
new_tab = tab.browser.new_tab(url)
person_info = new_tab.find_element_by_xpath('//table[@]').get_text()
alias = new_tab.find_element_by_xpath('//*[@id="ctl00_ContentPlaceHolder1_divAlias"]').get_text()
al = alias.replace('\n', ' ')
print(person_info)
print(al)
new_tab.close()
row = row 1
else:
break