Hey, I'm trying to stop the report message in terminal when selenium cant find element like the image when I don't finding elements:
talking about method: driver.find_elment(By.class_name,'name of element that doesn't exist')
I'm trying to make the message like in the image in the if\else
method (if cant find element it will print no such element or something like in the image in the else
part)
hope I'm clear (:
thanks
Asked
Active
Viewed 77 times
0

אסף אלפנדרי
- 1
- 1
-
Please copy the code and the message text here directly rather than posting a screenshot. – Code-Apprentice Feb 02 '22 at 23:58
-
donate-bsutton -> donate-button was a typo. Also just wrap it with len() and !=0 and it should be fine also you can use try except. – Arundeep Chohan Feb 03 '22 at 00:06
2 Answers
1
Wait for the element to be loaded. This is already answered.
Selenium - wait until element is present, visible and interactable
Edit:
The concept you should learn is exception control.
If you just want to "hide" the error:
try:
# Put here Code that will probably fail
# After that print success message
print("It worked")
except Exception as e:
# Uncomment this line to be able to see the error message
# print(str(e))
print("It failed because X reason")

Nuno Carvalho
- 167
- 4
-
hey @Nuno, I mean I want the element will not be found but I need to stop selenium notify about that and stop the message in the terminal (shown in the image) I'm trying change the message like in the else method (shown in the image) – אסף אלפנדרי Feb 02 '22 at 23:45
-
@אסףאלפנדרי This answer describes exactly how to do what you want. If it doesn't work for you, please show some more information to describe what the problem is. – Code-Apprentice Feb 03 '22 at 00:00
0
wait=WebDriverWait(driver,10)
driver.get('https://www.python.org/')
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.donate-button"))).click()
print('works')
except Exception as e:
#print(str(e))
print('fails')
Should display works if the element is clickable.
Another way is
elems=driver.find_elements(By.CSS_SELECTOR,"a.donate-button")
if len(elems)!=0:
print('work')
elem[0].click()
else:
print('fail')
Import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Arundeep Chohan
- 9,779
- 5
- 15
- 32