0

I am looping over a data frame with a for loop entering in information. Each time information is entered. Name and id. The database will return the information if it exists else nothing will be returned. How do I write an exception for when no information is return.

mylist=[]
for i,j in df1.iterrows():
    name = j[1]
    age = j[2]
    sleep(3)
    n = input(name)
    a = input(age)
    mylist1=[]
    result1= driver.find_element_by_css_selector("height")
    result2 = driver.find_element_by_css_selector("id")
    mylist1.append("height: ",result1)
    mylist1.append("id: ",result2)

print(tokens)
bumblebee
  • 1,811
  • 12
  • 19
devlops_s
  • 45
  • 2
  • 14
  • Can you please provide a sample, where there will be no information? – bumblebee Aug 05 '19 at 04:52
  • do you mean raising exception or using `try/except` ? – furas Aug 05 '19 at 05:01
  • @bumblebee I am using selenium webscraper to enter names with id number. This info is in the df. When the name and id is enter if the name and id exists in the database information will be return if no information exists nothing will show up. – devlops_s Aug 05 '19 at 05:02
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: – devlops_s Aug 05 '19 at 05:02

1 Answers1

0

after the for loop, you can do

if not mylist:
    raise Exception('The error message')

but raising such generic exception is considered as a bad practice, and should be avoided. instead you should use the most specific constructor that semantically fits your issue, so I suggest

if not mylist:
    raise ValueError('The error message')

for more details you can visite this link