0

code example image here

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

2 Answers2

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")

  • 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