0

I have a Clear Filter button on my UI that is removed after being clicked on.

My testcase is as follows:

1) Click the Clear Filter button

2) Check that the Clear Filter button has been removed from the UI

In the code below I am clicking the button and then I try to click it again, because it doesn't exist the test case should pass, but I am trying to verify the failure by attempting to click on it again, this doesn't seem like the best way to go about it...

 log_page.clear_filter_bttnclick()

    try:
        if log_page.clear_filter_bttnclick():
            testrailFunctions.failed()
    except NoSuchElementException:
        testrailFunctions.passed()

Can I use a try/except here to verify the button is removed or is there a better way about going about confirming that the Clear Filter button no longer exists on the UI?

Cheers

Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
Golshy
  • 63
  • 2
  • 13
  • Possible duplicate of [Python - Selenium WebDriver - Checking element exists](https://stackoverflow.com/questions/9567069/python-selenium-webdriver-checking-element-exists) – Grasshopper Aug 08 '17 at 16:02

1 Answers1

0

You can check if the element is present or not.

In python it should done something like below :-

from selenium.common.exceptions import NoSuchElementException        
def check_exists_by_xpath(xpath):
    try:
        webdriver.find_element_by_xpath(xpath)
    except NoSuchElementException:
        return False
    return True

Source :-

Python - Selenium WebDriver - Checking element exists

OR

methods return WebElement objects, not lists of objects. You'd have to do:

if len(browser.find_elements_by_xpath('//path')) == 0:
    print ('element not present')

Sorce :-

verify element existence selenium

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125