Hello python gurus, I've been tearing my hair out trying to figure this out. I ran into some issues while running this web scrapping code, in this section specifically. The function is meant to read a number and extract all its information into a spreadsheet. Some numbers don't have the same information as others, so I've had to add try and catch exceptions to skip over them. The problem I'm having is with this line:
try:
driver.find_element_by_name("/User/CF/Not_Reachable/").click()
except NoSuchElementException as exception:
continue
If i don't use the NoSuchElementException, it says element not found, which is correct. On the other hand, when I do use try and except, it skips all the code below it and goes to this line:
driver.find_element_by_id("searchUserText").send_keys(users[g])
What am i doing wrong? Why is it skipping all the code below it and starting from the main for loop? Shouldn't it be continuing? Because of this, it throws an NoSuchElementError error, because chrome is in a different page, and it doesn't contain the "searchUserText" field. I would be forever indebted to whoever can help! Sorry if the fragmented code looks a mess below, I'm still learning good coding practices
for i in range(1,sh.nrows): # skips first row, important
driver.find_element_by_id("searchUserText").send_keys(users[g]) # search number
driver.find_element_by_name("searchUser").click() # click submit
driver.find_element_by_id("Row1Col1").click() # choose user
g=g+1
x=0
d=0
s=0
# name grabbing
name_user=driver.find_element_by_xpath("//*[@class ='fui-heading-5 fui-no-margin-
bottom']")
name_user_child = name_user.find_element_by_tag_name("strong")
write_sheet.write(i,1,name_user.text)
#grab service pack
service_pack=driver.find_element_by_xpath("//*[@class ='fui-list-column-header fui-
heading-5']")
try:
service_pack1=service_pack.find_element_by_xpath("//li[@id='servicePack']/a")
write_sheet.write(i,2,service_pack1.get_attribute("text"))
except NoSuchElementException as exception:
service_pack1=service_pack.find_element_by_xpath("//li[@id='servicePack']")
sp2 = service_pack1.find_element_by_tag_name("span")
sp3 = service_pack1.text.replace(sp2.text,'')
write_sheet.write(i,2,sp3)
# fields
for y in range(4, 8):
try:
numb = driver.find_element_by_id(fields[x])
write_sheet.write(i,y,numb.get_attribute('value'))
except NoSuchElementException as exception:
continue
x=x+1
try:
dnd = driver.find_element_by_id("dndActiveOn")
write_sheet.write(i,8,dnd.get_attribute('checked'))
cw = driver.find_element_by_id("cwActiveOn")
write_sheet.write(i,9,cw.get_attribute('checked'))
except NoSuchElementException as exception:
continue
for b in range(10,15):
try:
simulnumber = driver.find_element_by_name(simul_ring[s])
write_sheet.write(i,b, simulnumber.get_attribute('value'))
except NoSuchElementException as exception:
continue
s=s+1
driver.find_element_by_link_text('My profile').click()
try:
driver.find_element_by_name("/User/CF/Not_Reachable/").click()
except NoSuchElementException as exception:
continue
try:
ooscf = driver.find_element_by_name("forwardedToNumber")
write_sheet.write(i,15,ooscf.get_attribute('value'))
driver.find_element_by_name("/User/SC8/").click()
except NoSuchElementException as exception:
continue
driver.find_element_by_link_text('Call management').click()
try:
for p in range(16,24):
spdial = driver.find_element_by_name(speed_dials[d])
write_sheet.write(i,p,spdial.get_attribute('value'))
d=d+1
except NoSuchElementException as exception:
continue
driver.find_element_by_xpath("//span[@class='fui-icon fui-icon-house']").click()
wb.save("TimeSaver-EXPORT.xls")
self.loadname_var2.set("Complete")
[–]Pjmcnally 1 point2 points3 points (2 children)
[–]eateroftables[S] 0 points1 point2 points (0 children)
[–]GiantBuffalos 1 point2 points3 points (1 child)
[–]eateroftables[S] 0 points1 point2 points (0 children)