user3695968
user3695968

Reputation: 33

How to simulate onclick (JavaScript) using Selenium/Python

I am using Python/Selenium to click on an icon on a web site which downloads a file. I know how to click on regular buttons using Selenium but this one is a bit tricky as it's not a regular button and it's making a Javascript call. I've tried several find_element_by calls but was unable to access this element. Can anyone think of a way to click on this using a selenium call?

When I do inspect element for the download icon in my web browser this is what I get:

<a href="javascript: void(0)" class="pull-right margin-r" onclick="
                        document.theForm.action='/p1234/DownloadData';
                        $('#theForm').append($('<input>', {type:'hidden', name:'Download', value:'Download'})).submit();
                    "><img src="/images/download.png" title="Download" alt="Download" style="" border="0"></a>

thanks in advance

Upvotes: 3

Views: 10819

Answers (1)

alecxe
alecxe

Reputation: 473863

How about locating the element by the part of the onclick attribute:

driver.find_element_by_css_selector("a[onclick*=DownloadData]").click();

where *= means "contains".

Upvotes: 2

Related Questions