Check If element exists in python selenium

10,370

A good way is to use an explicit wait. An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. Look at the example:

self.ui.jobs.setJobTitleEleName(titleEleName, titleText)
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 5)

try:
    wait.until(EC.visibility_of_element_located((By.NAME, dropdownName)))
    self.ui.jobs.selectButton(dropdownName)
    self.ui.sleep(4)
    self.ui.jobs.selectAor(dropdownOptn)
except TimeoutException:
    self.ui.jobs.selectAddAors(dropdownOptn)
    self.ui.sleep(4)

self.ui.jobs.selectButton(sbmtBtnName)

The solution with if / else statement:

def provideDetails(self, titleEleName, titleText, sbmtBtnName, dropdownOptn, dropdownName):
    self.ui.jobs.setJobTitleEleName(titleEleName, titleText)
    browser = webdriver.Chrome()
    wait = WebDriverWait(browser, 5)
    elem = wait.until(EC.visibility_of_any_elements_located((By.NAME, dropdownName))) # will return a list of elements

    if elem:
        self.ui.jobs.selectButton(dropdownName)
        self.ui.sleep(4)
        self.ui.jobs.selectAor(dropdownOptn)
    else:
        self.ui.jobs.selectAddAors(dropdownOptn)
        self.ui.sleep(4)

    self.ui.jobs.selectButton(sbmtBtnName)

An expectation for checking that there is at least one element visible on a web page. locator is used to find the element returns the list of WebElements once they are located.

Imports:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
Share:
10,370
Sidharth Gokul
Author by

Sidharth Gokul

Updated on June 04, 2022

Comments

  • Sidharth Gokul
    Sidharth Gokul almost 2 years

    Following is my python selenium code

    def provideDetails(self, titleEleName, titleText, sbmtBtnName, dropdownOptn, dropdownName):
    
            self.ui.jobs.setJobTitleEleName(titleEleName, titleText)
    
            elem = self.ui.jobs.selectButton(dropdownName)
    
            if elem.is_displayed():
                self.ui.jobs.selectButton(dropdownName)
                self.ui.sleep(4)
                self.ui.jobs.selectAor(dropdownOptn)
            else:
                self.ui.jobs.selectAddAors(dropdownOptn)
                self.ui.sleep(4)
    
            self.ui.jobs.selectButton(sbmtBtnName)
    

    I have to check, if the 'elem' is present or not. And if it is present, 'if' condition should happen, and if not, 'else' condition should work. I tried this code. And I got this error "Attribute error: None Type object has no attribute 'is_displayed'. Any help would be appreciated. Thanks.

    Also, Is there any alternative method to check if an element exists and follow with the if else commands

    • eduPeeth
      eduPeeth almost 6 years
      Can you try checking what elem holds before getting into the loop?
    • Andersson
      Andersson almost 6 years
    • Sidharth Gokul
      Sidharth Gokul almost 6 years
      @eduPeeth elem clicks the button.
    • murali selenium
      murali selenium almost 6 years
      provide code in selectButton method
    • Sidharth Gokul
      Sidharth Gokul almost 6 years
      @muraliselenium def _selectSpan(self, span): """Clicks a span by text - submit, cancel, etc.""" self.ui.driver.find_element_by_xpath( "//span[contains(text(),\'{}\')]".format(span)).click() def selectButton(self, button): """Clicks a button in the Provide details page for Job, Crew, etc.""" self._selectSpan(button)
    • Sidharth Gokul
      Sidharth Gokul almost 6 years
      or is there any alternative way to check whether an element exists ? and follow with the if else commands
  • Andersson
    Andersson almost 6 years
    If element is found it doesn't mean that it is displayed. Do not mislead OP. You can update your code as if elem and elem[0].is_displayed():
  • Oleksandr Makarenko
    Oleksandr Makarenko almost 6 years
    @Andersson I'm sorry. I have rewrote my answer according to best practices. Please, check, I'm interested in your opinion.
  • Andersson
    Andersson almost 6 years
    OK. But now it seem that the solution doesn't meet OP requirement to use if/else construction
  • Oleksandr Makarenko
    Oleksandr Makarenko almost 6 years
    I see. Let's leave two solutions for consideration.