I wrote a Python script that can automate the filling of data into a form. I used Selenium ChromeDriver for this purpose. The execution of the program is not normal, as I happen to encounter an error message every time.
I want the code snippet below to check if a table is present on the website. If yes, execute whatever is below the if statement, else execute the continue statement.
The problem is that the table that the program is checking may/may not be there on the website that I am using.
This is the part of the code that I am concerned about:
if(browser.find_element_by_xpath('/html/body/div/div/center/table/tbody').is_displayed()):
print("Result Found for Roll No: " + str(rollNo))
anotherResultLink = browser.find_element_by_link_text('Check Another Result').click()
result_feeder(rollNo + 1, True)
else:
continue
And this is the error message thrown at me every time:
Message=Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/center/table/tbody"}
(Session info: chrome=84.0.4147.125)
Source=E:\Mithun\Python\Website Automator\Website Automator\Website_Automator.py
StackTrace:
File "E:\Mithun\Python\Website Automator\Website Automator\Website_Automator.py", line 61, in admit_card_generator
if(browser.find_element_by_xpath('/html/body/div/div/center/table/tbody').is_displayed()):
File "E:\Mithun\Python\Website Automator\Website Automator\Website_Automator.py", line 39, in result_feeder
admit_card_generator(rno)
File "E:\Mithun\Python\Website Automator\Website Automator\Website_Automator.py", line 64, in admit_card_generator
result_feeder(rollNo + 1, True)
File "E:\Mithun\Python\Website Automator\Website Automator\Website_Automator.py", line 33, in result_feeder
admit_card_generator(list_of_roll_nos[i])
File "E:\Mithun\Python\Website Automator\Website Automator\Website_Automator.py", line 74, in <module>
result_feeder(20607526, False)
Here's the thing that disturbs me about this program. I know for a fact that the is_displayed() function returns a boolean value. So, the if statement at the beginning of the program checks if the table (which may/may not be there in the website) is present or not.
However, it looks as though Python is refusing to even check the if statement, whenever the website that I am using does not have the table. As a result, I am encountering this error.
The question that I have is: How do I force Python to check if a table exists (using the is_displayed() function), even though the table does not exist?
Any solutions/workarounds to this problem are greatly appreciated.
P.S.: I am a Grade 12 student.