58676

Selenium clicking a link where the href is javascript:void[0];

I'm trying to see if when a link is clicked a report is generated on the page using Selenium WebDriver with Java.

What happens is there are a bunch of links on a page, most of them when clicked go to another page where I have automated filling in the details and submitting the form to check the report is generated.

However, there are three reports that when clicked cause some JS to run and after a few seconds, a new link is displayed on the same page to download the report.

How can I click this link?

I am reading all the links on the page into a list of WebElements then looping through them for where the href contains javascript: to void[0];

I then try calling the click method on that element in the list, allLinks.get(i).click();

This is the same thing I do for the other reports and it works fine but on these 3 I get an error "Element is not currently visible and so may not be interacted with"

Using firebug if I inspect the link that starts the JS running it says:

<a onclick="requestReportGeneration('2cad4d4e5c8855c47a88b6ddf8345735', 'reportDiv33','CSV')" href="javascript:void[0];">CSV</a>

Can anyone suggest a way to click the link?

The page contains lots of links that say CSV so I can't just use the link text.

<strong>UPDATE:</strong> I've just had a thought about this which might help. When I first come to the page with the report links it will say "Order reports" I need to click the heading which then calls a JS function to expand that section and display the links.

The reports that work because when I come to this page I just read all the hrefs of the page source and do driver.get(reportList.get(i); so I'm not actually clicking on the link.

I have added a link to get the xpath of the heading and click it but then when I try to click the link with the href or javascript: void I still get an error saying it's not visible.

Answer1:

Check if this command clicks any (actually 1st with javascript:void[0] ) link

driver.findElement(By.xpath("//a[contains(@href,\"javascript:void[0]\")]")).click();

If yes - then something wrong with iterator

Answer2:

For anchor tags that contain href="javascript:" use native javascript click() method like this:

WebElement wwwLink = ((FindsByCssSelector) webDriver) .findElementByCssSelector("table[id='initiatorsTable'] tbody tr:not([class^='empty']) td[class='first'] a"); JavascriptExecutor exec = (JavascriptExecutor) webDriver; exec.executeScript("arguments[0].click()", wwwUrl); //TODO: wait for action code ...

Recommend

  • Vanilla JS change active state of links when scrolling refactoring
  • Anchor Link to Multiple Locations using jQuery
  • I can't grab specific URL in search page
  • Use a ListProperty or custom tuple property in App Engine?
  • How to handle database exceptions/ issue?
  • Python : Find tuples from a list of tuples having duplicate data in the 0th element(of the tuple)
  • Explode string except where surrounded by parentheses?
  • Reset all checkboxes on page without looping
  • How to check for presence of the key of a value (as defined in one dict) in another dict, in Python?
  • create a matrix from array of elements under diagonal in numpy
  • FOR /R - Using set /a [variable] = [variable] + %%~zA
  • Compare Pandas dataframes and add column
  • Web browser lock-down: How to?
  • Why can't UI components be accessed from a backgroundworker?
  • How to getText() from the input field of an angularjs Application
  • Hide HTML elements without javascript, only CSS
  • Python pickle not one-to-one: different pickles give same object
  • bad substitution shell- trying to use variable as name of array
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Jquery UI tool tip close icon
  • Meteor helpers not available in Angular template
  • HTML download movie download link
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Updating server-side rendering client-side
  • ILMerge & Keep Assembly Name
  • Weird JavaScript statement, what does it mean?
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Django query for large number of relationships