2

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

Sidharth Gokul
  • 111
  • 1
  • 6
  • 14
  • Can you try checking what `elem` holds before getting into the loop? – eduPeeth Jul 24 '18 at 07:24
  • Possible duplicate of [Check if element exists python selenium](https://stackoverflow.com/questions/45695874/check-if-element-exists-python-selenium) – Andersson Jul 24 '18 at 07:25
  • @eduPeeth elem clicks the button. – Sidharth Gokul Jul 24 '18 at 07:42
  • provide code in selectButton method – murali selenium Jul 24 '18 at 07:46
  • @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 Jul 24 '18 at 07:51
  • or is there any alternative way to check whether an element exists ? and follow with the if else commands – Sidharth Gokul Jul 24 '18 at 07:52

2 Answers2

2

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
Oleksandr Makarenko
  • 779
  • 1
  • 6
  • 18
0

As per comment selectButton will perform click action but not returning web element. Let me explain in java

WebElement e = driver.findElement(By.xpath("//elementPath"));

Here is e is web element on which i can click

 e.click();

Or can verify if element is displayed or not (in if condition)

e.isDisplayed(); 

but WebElement e = driver.findElement(By.xpath("//elementPath")).click(); is not valid one. In Java it show you exception in editor like eclipse as soon as you write it. so driver.findElement(By.xpath("//elementPath")).click(); will not return to element to check is displayed or not.

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
murali selenium
  • 3,847
  • 2
  • 11
  • 20