Home > other >  Selenium - how to check that button is HIDDEN, without throwing error? (python)
Selenium - how to check that button is HIDDEN, without throwing error? (python)

Time:11-23

I'm trying to do the test to learn Allure, and to assure that the test is passed, the button has to be INVISIBLE. It first clicks 1st button to make 2nd button appear. Then click 2nd button - so same (2nd button disappears). Here it is: http://the-internet.herokuapp.com/add_remove_elements/

My code would look like this (below), it clicks the 1st button, 2nd button - and after it should check that button DELETE is not visible anymore. Instead it interrupts whole code and throws error that element was not found/located. How do you make it so it will not interrput/cancel whole codeblock when it doesn't find this button?

class TestPage:

    def test_button(self):
        s=Service('C:\Program Files\chromedriver.exe')
        browser = webdriver.Chrome(service=s)
        browser.get("http://the-internet.herokuapp.com/")
        browser.maximize_window()
        time.sleep(1)
        add = browser.find_element(By.XPATH, "/html/body/div[2]/div/ul/li[2]/a")
        add.click()
        time.sleep(1)
        button = browser.find_element(By.XPATH, "/html/body/div[2]/div/div/button")
        button.click()
        time.sleep(1)
        deleteButton = browser.find_element(By.XPATH, "/html/body/div[2]/div/div/div/button")
        deleteButton.click()
        deleteCheck = browser.find_element(By.XPATH, "/html/body/div[2]/div/div/div/button").is_displayed()
        if deleteCheck == False:
            assert True
        else:
            assert False
        time.sleep(1)
        browser.close()

Here's edited code (with last step trying to go to main page):

def test_button(self):
        s=Service('C:\Program Files\chromedriver.exe')
        browser = webdriver.Chrome(service=s)
        browser.get("http://the-internet.herokuapp.com/")
        browser.maximize_window()
        wait = WebDriverWait(browser, 3)
        time.sleep(1)
        add = browser.find_element(By.XPATH, "/html/body/div[2]/div/ul/li[2]/a")
        add.click()
        time.sleep(1)
        button = browser.find_element(By.XPATH, "/html/body/div[2]/div/div/button")
        button.click()
        time.sleep(1)
        browser.save_screenshot('buttons.png')
        time.sleep(1)
        delete = browser.find_element(By.XPATH, "/html/body/div[2]/div/div/div/button")
        delete.click()
        time.sleep(1)
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick*='delete']"))).click()
        time.sleep(0.5)
        if not browser.find_elements(By.CSS_SELECTOR, "button[onclick*='delete']"):
            assert True
        else:
            assert False
        time.sleep(0.5)
        browser.get("http://the-internet.herokuapp.com/")

CodePudding user response:

After clicking the delete button it disappears. Not only becomes invisible but no more present on the page.
The clearest way to validate web element is not presented is to use find_elements method. This method return a list of elements found matching the passed locator. So, in case of match the list will be non-empty and in case of no match the list will be empty. Non-empty list is indicated by Python as Boolean True, while empty list is indicated as Boolean False. No exception will be thrown in any case.
Additionally, you need to improve your locators and use WebDriverWait expected_conditions explicit waits, not hardcoded sleeps.
The following code worked for me:

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

url = "http://the-internet.herokuapp.com/add_remove_elements/"
driver.get(url)


wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick*='add']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick*='delete']"))).click()
time.sleep(0.5)
if not driver.find_elements(By.CSS_SELECTOR, "button[onclick*='delete']"):
    print("Delete button not presented")

CodePudding user response:

you can try this code to wait for that element in 15 second and if it does not appear then continue code

try:
    WebDriverWait(self.browser, 15).until(EC.visibility_of_all_elements_located((By.XPATH, "/html/body/div[2]/div/div/div/button")))
    ...
except:
    continue

CodePudding user response:

You can wrap the deleteCheck in a try block:

try:
    deleteCheck = browser.find_element(By.XPATH, "/html/body/div[2]/div/div/div/button")
    assert False
except NoSuchElementException:
    assert True
  • Related