简体   繁体   English

Selenium 单击 href 为 javascript:void[0]; 的链接。

[英]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.我正在尝试查看是否在单击链接时使用带有 Java 的 Selenium WebDriver 在页面上生成报告。

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.但是,有三个报告,单击时会导致某些 JS 运行,几秒钟后,同一页面上会显示一个新链接以下载报告。

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];我正在将页面上的所有链接读入 WebElements 列表中,然后循环遍历它们以查找 href 包含 javascript: to void[0]; 的位置。

I then try calling the click method on that element in the list, allLinks.get(i).click();然后我尝试调用列表中该元素的 click 方法,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"这与我对其他报告所做的相同,它工作正常,但在这 3 个报告中,我收到错误消息“元素当前不可见,因此可能无法与之交互”

Using firebug if I inspect the link that starts the JS running it says:如果我检查启动 JS 运行的链接,则使用 firebug 说:

<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.该页面包含许多表示 CSV 的链接,因此我不能只使用链接文本。

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.当我第一次来到带有报告链接的页面时,它会说“订购报告”,我需要单击标题,然后调用 JS 函数来展开该部分并显示链接。

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.报告之所以有效,是因为当我来到这个页面时,我只是阅读了页面源的所有 href 并执行 driver.get(reportList.get(i); 所以我实际上并没有点击链接。

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.我添加了一个链接来获取标题的 xpath 并单击它,但是当我尝试单击带有 href 或 javascript: void 的链接时,我仍然收到一条错误消息,指出它不可见。

Check if this command clicks any (actually 1st with javascript:void[0] ) link检查此命令是否单击任何(实际上是第一个javascript:void[0] )链接

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:对于包含href="javascript:"锚标记,请使用本机 javascript click() 方法,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM