Selenium WebDriver - Click a Javascript Button

Viewed 472

This is a button I want to click :

<a id="BUTTON_GROUP_ITEM_A5_btn4_acButton" ct="B" st="" href="javascript:void(0);" ti="0" tabindex="0" class="urBtnStd" ocl="sapbi_buttonGroup_callCustomScript([['CUSTOMFUNCTION','ExportXLSCM',0]],event);" onkeydown="ur_Button_keypress(event);" onclick="ur_Button_click(event);" style="min-width: !important;text-align:center;overflow:visible;">Export to Excel</a>

I've used the following methods:

button = driver.find_element_by_link_text('Export to Excel')
button.click()

button = driver.find_element_by_xpath("//a[contains(@id, 'BUTTON_GROUP_ITEM_A5_btn4_acButton')]")  
button.click()

#Using full xPath
button = driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/td/div[2]/div[1]/div/table[1]/tbody/tr/td[3]/div/span[5]/a")
button.click()

but they all give selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element error

Any idea how to fix this or to even print all the elements on the page so that I can find out what to 'click'? I've tried so many ways but all are giving the same error. Thanks!

enter image description here

2 Answers
button = driver.find_element_by_link_text('Export to Excel')
button.click()

Explanation :

If the above gives you NoSuchElementException, I would probably suspect this it is in iframe, if it happens to be then in that case you would need to switch to iframe first and continute with this web element.

Code :

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "iframe xpath here")))
wait.until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Export to Excel"))).click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

if this does not work, I would probably ask about your driver configuration. Is it opening browser in full screen ?

Related