I need to check that a certain element is present on the web page.

I find the item as follows:

Element = driver.find_element_by_id('text') 

Check done as follows:

 self.assertTrue("id = text", Element) 

But, I was told that this is not correct, because this code does not check anything: the first parameter of the method is the test, and the second is just a message that is output when it falls.

After that, I found how to check the availability of items by the following method:

 from selenium.common.exceptions import NoSuchElementException def check_exists_by_id(id): try: webdriver.find_element_by_id(id) except NoSuchElementException: return False return True 

Now I am generally confused. Please tell me which method still needs to check the presence of an element on the page? And yet, is it possible to do this through assert.True() ?

    1 answer 1

     from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC pagination_block = WebDriverWait(driver, 10).until(EC.visibility_of_element_located( (By.CSS_SELECTOR, "span.b-pagination__item.b-pagination__item--next.js-pagination-next"))) 

    Expects that within 10 seconds this element will be located and visible. If it is not found within 10 seconds, throws a Timeout error. Returns the item as soon as found , but no later than 10 seconds

    EC has many conditions for finding an element, look and select the one you need.