Python Selenium – Getting TimeoutException for elements belonging to a particular section of a webpage

Question:

I’ve been trying to click this particular button that belongs to a toggle switch in a website.

<button _ngcontent-plj-c265="" type="button" class="glyphicon glyph-mini ng-star-inserted expand" aria-expanded="true" aria-label="Title Expand or collapse filter card" style="visibility: visible;" pbi-focus-tracker-idx="6"></button>

This exists in the "filter" section of the website and I’ve adopted several methods and tags to perform this operation, some of which I’ve put here:

1. sample1 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("xpath", '//*[@id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]'))).click()

2. sample2 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("css selector", 'button.expand'))).click()

3.sample3 = driver.find_element("xpath",'//[@id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]').click()

Method 3 gave me a NoSuchElementException. Hence, I adopted methods 1 and 2 but got a TimeoutException for both. The button has no ID, so I couldn’t fetch it that way, too.

But what I noticed was that there is an attribute in the button, aria-expanded="true" which when, I’m assuming, taking a value "false" will help operate the toggle button. But the catch is it can only do that if Selenium identifies the element itself, which brings us back to square one.

I’d highly appreciate any fruitful answer for my predicament. Thanks in advance.

Asked By: Rex

||

Answers:

I was able to solve my problem by switching to the <iframe> the element was within using this:

driver.switch_to.frame(0)   #switching to the iframe here
tb = driver.find_element("xpath", '//*[@id="exploreFilterContainer"]/div[2]/div/filter[15]/div/div[1]/div[1]/button[1]')
driver.execute_script("arguments[0].click();", tb) #using JS to click the button

and it’s advised that after all the operations pertaining to the element is performed that we switch back to normal as:

driver.switch_to.default_content()

Thanks to @Prophet @simpleApp for your efforts in trying to help me out 🙂

Answered By: Rex