2

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.

UPDATE: 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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Doctor Who
  • 1,287
  • 5
  • 25
  • 46
  • Are you maybe trying to click on the underlying link to the report before the link that should make it visible? – so cal cheesehead Apr 30 '13 at 15:26
  • No I'm trying to click on the link CSV that will then make some JavaScript run on the page to generate the report. The CSV link is always there once clicked a loading circle is displayed and a new link named Download report which I would then check but I can't even get as far as clicking the CSV link – Doctor Who Apr 30 '13 at 15:45
  • Can you provide a link to the page, or at least its source? – acdcjunior Apr 30 '13 at 17:01
  • Sorry, the source wasn't showing up correctly in the question as I didn't wrap it in code tags. I have updated it now, please see above. Unfortuantly the page is internal to work at the minute and I can't give the whole page source for security reasons. The code I have added to the question is inside a table but the table doesn't have a name if that would have helped. – Doctor Who May 01 '13 at 07:07

2 Answers2

0

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

dimkin
  • 108
  • 1
  • 11
  • I have just tried that but I get the same error saying the element is not currently visible. If I use firebug on the link I can see the xpath is .//*[@id='mainarea']/div[2]/fieldset[3]/div/table/tbody/tr[1]/td[2]/a Would it be possible to use that? I kind of want to avoid xpath as this test will be run on multiple sites. I havent checked but I don't know if the xpath for the three reports with the javascript:void href would be the same on each site. – Doctor Who May 02 '13 at 15:07
  • I have added some more info to the question that may explain why this didn't work. Please could you take a look? – Doctor Who May 03 '13 at 07:40
  • just add extra step , which clicks link (heading) and some wait to results be loaded, before clicking javascript:void – dimkin May 13 '13 at 07:34
0

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 ...
azec-pdx
  • 4,790
  • 6
  • 56
  • 87