2

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 2

1
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 ?

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect!! that worked like a charm..you saved me endless hours of misery :-) but what I didn't get is why do I need to switch to the iframe? why didn't the full XPath work for example? Is there a way to work on ANY XPath without switching or any additional work?
0

I found a far easier solution using Helium library:

pip install helium

More info: https://selenium-python-helium.readthedocs.io/en/latest/api.html

from helium import *
start_chrome(url)
click('Export to Excel')

It's that simple! Helim is very simple and user friendly. Highly recommended.

4 Comments

This ia unethical. Helium was not tagged. Do not ever do this !
I'm not sure what's unethical about that?!! The point is having the best solution for the developers community and clearly this is a simpler solution given the overall objective. I didn't know about Helium and if I knew it was that easy that would have saved me hours. Having said that I'm grateful for your help and accordingly I've returned back your answer as the accepted answer. I hope this makes you happy. Thanks.
No, at initial phase it was never mentioned that you are looking for a different library, the original question was to click on a JS button, Plus you had tagged Selenium . You can do a lot of stuff with Selenium.. whereas Helium's API are just for Python and mainly works in FF and Chrome. So if somebody is running into same issue but if it's a safari browser then Helium won't work . yes you do not need to switch to iframes in Helium. But it has it's own disadvantage. But if you really think that your solution should be an accepted answer, go ahead.. mark it as accepted answer.
Thanks for the feedback. Both answers are there so it's up to others to choose what works best for them. I want to give you the credit you deserve so I'll happily keep your answer as the accepted one. Thanks ☺️

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.