Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98817

How to apply multiple conditional statements in selenium webdriver python

$
0
0

I am fairly new to Selenium, I am trying to find a Contact info element in a page, and click it if it exists. Many times, what happens is, the element is in all caps such as CONTACT, sometimes Contact and sometimes contact. So I've stored these cases in a variable and I'm using find_element_by_partial_link_text to find the right element and click on it. I'm using exception handling (try and except) and if loop to check each condition. This is my code:

from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException

browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.maximize_window()

ab = 'Contact'
bc = 'CONTACT'
cd = 'contact'

browser.get('https://www.dominos.co.in/store-location/pune')

try:
    if browser.find_element_by_partial_link_text(ab).is_displayed():
        browser.find_element_by_partial_link_text(ab).click()

    elif browser.find_element_by_partial_link_text(bc).is_displayed():
        browser.find_element_by_partial_link_text(bc).click()

    elif browser.find_element_by_partial_link_text(cd).is_displayed():
        browser.find_element_by_partial_link_text(cd).click()

except NoSuchElementException:
    print("No such element found")
    browser.close()

So if Contact element is present in any webpage, this code is able to click on it, but if other two elements are present, it goes directly into except and prints No such element found. If you guys could help me tackle this scenario, I would really appreciate it :)


Viewing all articles
Browse latest Browse all 98817

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>