最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Selenium clicking a link where the href is javascript:void[0]; - Stack Overflow

programmeradmin0浏览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.

UPDATE: I've just had a thought about this which might help. When I first e 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 e 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.

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 e 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 e 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.

Share Improve this question edited Feb 25, 2017 at 16:03 Anonymous 86.6k15 gold badges162 silver badges178 bronze badges asked Apr 30, 2013 at 15:09 Doctor WhoDoctor Who 1,6678 gold badges41 silver badges57 bronze badges 4
  • Are you maybe trying to click on the underlying link to the report before the link that should make it visible? – so cal cheesehead Commented Apr 30, 2013 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 Commented Apr 30, 2013 at 15:45
  • Can you provide a link to the page, or at least its source? – acdcjunior Commented Apr 30, 2013 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 Commented May 1, 2013 at 7:07
Add a ment  | 

2 Answers 2

Reset to default 0

Check if this mand 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

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 ...
发布评论

评论列表(0)

  1. 暂无评论