Ashley Westbrook
Ashley Westbrook

Reputation: 11

Unable to click on button using the Selenium library in Python because element is hidden (Only able to do so using JavaScript)

<a class="btn btn-xs btn-success" href="/user-location-auth/location?id=4013" 
title="View User Location Access" data-toggle="tooltip" data-method="post"> 
<span class="glyphicon glyphicon-map-marker"></span></a>    <span 
class="glyphicon glyphicon-map-marker"></span>        <<pseudo:before>> 
</<pseudo:before>>

Hi all, I have been tasked with a project that requires clicking on a button in a table (with 14 cells). The first 13 cells are all unhidden, but the 14th one (where the buttons are located), are hidden unfortunately. I have tried action chains, I have tried hovering over the element and then clicking on it. I seem to be at a loss here. This piece of code did work, however I want to avoid using javascript as I want to replicate a user experience.

z4 = driver.find_element_by_css_selector('a.btn-success > span.glyphicon-map-marker')
driver.execute_script('arguments[0].click();', z4)

Upvotes: 0

Views: 103

Answers (1)

Ashley Westbrook
Ashley Westbrook

Reputation: 11

Hi everyone and cruisepandey, I found a solution that works. First, I waited for the presence of the element to be located. Once that occurred, I grabbed the element and moved to it with ActionChains (I think I was misusing ActionChains earlier). Then I was able to click on the Bootstrap button.

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, """
//tr[@data-key='2312312321321']/td/a[contains(@class, 'btn-xs btn- 
success')]/span[contains(@class, 'glyphicon')]""")))

y = driver.find_element(By.XPATH, """//tr[@data- 
key='2312312321321']/td/a[contains(@class, 'btn-xs btn- 
success')]/span[contains(@class, 'glyphicon')]""")

#Use action chains to click on the button
ActionChains(driver).move_to_element(y).click().perform()

Upvotes: 1

Related Questions