Home > Blockchain >  how do I stop selenium make this report
how do I stop selenium make this report

Time:02-03

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

CodePudding user response:

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

CodePudding user response:

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
  •  Tags:  
  • Related