Reputation: 1
I am trying to click on a link that is generated dynamically and seems to execute some Javascript. I have tried to use the 'onclick' and JavaScripExecutor methods but I am not able to locate the element in the page. The snippet of code that I am working with is as follows:
<a class="a68a" target="_top" style="cursor:pointer;" href="" onkeypress="if(event.keyCode == 13 || event.which == 13){ClientReportc2b21bbce37e4f5ba98575a2680610a2.ActionHandler('Drillthrough','64iT0R0x0:0');}return false;" onclick="ClientReportc2b21bbce37e4f5ba98575a2680610a2.ActionHandler('Drillthrough','64iT0R0x0:0');return false;" tabindex="1">
<div class="r13" style="WIDTH:17.42mm;">
<div class="a67"> <span class="a66">123456789</span> </div>
</div>
</a>
I have tried a number of different ways but to no avail:
(a) driver.findElement(By.linkText("ClientReport8522bb9804044e969553e386b7010c6d.ActionHandler('Drillthrough','64iT0R0x0:0')")).click();
(b) driver.findElement(By.xpath("//a[@onclick='ClientReport8522bb9804044e969553e386b7010c6d.ActionHandler('Drillthrough','64iT0R0x0:0')']")).click();
(c) WebElement element = driver.findElement(By.xpath("//a[@class='a68a']"));
element.click();
(d) WebElement element= driver.findElement(By.xpath("//a[@class='a68a']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
I think (a) and (b) fail because part of the JS name is dynamically generated and I cannot rely on that. I would be most grateful for some help. Thanks.
Upvotes: 0
Views: 3003
Reputation: 6962
If the element is dynamically generated then you should be probably waiting for the element to appear first and then click on it. Try below code to see if it helps -
driver.wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@class='a68a']"))).click();
Hope this helps.
Upvotes: 2