Selenium Java - 尝试单击链接时的ElementNotInteractableException

时间:2017-07-09 02:00:37

标签: java selenium click

在该计划中,我试图通过搜索包含单词" team"的链接来点击来自各种网站的链接。我在我尝试过的几个网站上收到错误,而不是在其他网站上,我知道为什么和任何修复可用? 我的代码段如下:

if (driver.getPageSource().contains("Team"))
{
     driver.findElement(By.partialLinkText("Team")).click();      
     return;
}

这适用于某些网站,但在其他网站上我

Exception in thread "AWT-EventQueue-0" org.openqa.selenium.ElementNotInteractableException:

我不知道这个问题的根源,因为我需要它在多个页面上工作,例如我在https://www.calipercorp.com/about-us/上得到了回复

非常感谢任何变通办法或帮助。

2 个答案:

答案 0 :(得分:0)

我们试试这个:

if (driver.getPageSource().contains("Team"))
{
     WebElement elem = driver.findElement(By.partialLinkText("Team"));      
     //Check if the element's type is a link
     if (elem.getTagName().equals("a"){
          elem.click();
     }
     return;
}

或者您可以使用方法getAttribute("href")获取链接网址,然后转到此链接而不是点击它

if (driver.getPageSource().contains("Team"))
    {
         WebElement elem = driver.findElement(By.partialLinkText("Team"));      
         //get the href attr
         String url = elem.getAttribute("href");         
         //add code to go to the url above
    }

答案 1 :(得分:0)

如果您想查找带有部分文字的链接。你的方法很费时间。

我建议使用xpath,因为它很简单且代码较少。

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

有时元素不可见/可点击。有时href也是相对的。要通过Javascript点击

来解决这个问题

要检查元素使用的存在findElements请不要使用pageSource。

List<WebElement> teamEls=driver.findElement(By.xpath("//a[contains(text(),'Team')]"));
if(!teamEls.isEmpty())
{
   WebElement team = teamEls.get(0);
   if(team.isDisplayed())
   {
      team.click();
   }
   else
   {
       JavascriptExecutor js = (JavascriptExecutor) driver;
       js.executeScript("arguments[0].click();", team);
   }


}
相关问题