<a href="javascript:viewDocument(&#39;122&#39;, &#39;212&#39;);"> is not accessible. I am not sure why the link is not clickable

时间:2017-09-25 17:58:39

标签: selenium webdriver

<div id="myDiv" style="display: block;">
 <table>
  <tr>
   <td>
    <a href="javascript:viewDocument('122', '212');">
     View Document
    </a>
   <td>
  <tr> 
 </table>
</div>

I am not able to click the a tag which opens up a document. Sometime it worked when I used

driver.findElement(By.xpath(//a[contains(. = 'View Document')]))

But is is not working all the time. And I would also like to know is there any way to run the javascript in the href directly? I already tired using Javascript Executor.

I am getting this error:- The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.

1 个答案:

答案 0 :(得分:0)

使用此xpath工作

 //a[contains(text(),'View')]    

它对我有用

driver.findElement(By.xpath("//a[contains(text(),'View')]")).click();

当您更改页面时,您可能会尝试搜索元素,这就是为什么会出现过时错误,您必须回到sape页面才能找到它。我已经为它创建了一个演示。首先单击链接并再次进入主页单击并导航,然后不会发生错误。

for(int i=0;i<=3;i++)
    {
    driver.findElement(By.xpath("//a[contains(text(),'View')]")).click();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.navigate().back();
    }