【问题标题】:ElementNotVisibleException: Element is not currently visible and so may not be interacted withElementNotVisibleException:元素当前不可见,因此可能无法与之交互
【发布时间】:2016-11-13 09:12:23
【问题描述】:

我收到了ElementNotVisibileException,但此页面没有名称为“历史”的重复名称,但它确实有“通话历史”。谁能推荐最好的定位器来定位元素?

<li class="ui-widget ui-menuitem ui-corner-all ui-menu-parent" aria- haspopup="true" role="menuitem">
   <a class="ui-menuitem-link ui-corner-all" href="javascript:void(0)">
      <span class="ui-menuitem-text">History</span>
      <span class="ui-icon ui-icon-triangle-1-e"></span>
   </a>

错误

org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:122 毫秒构建信息:版本:'2.52.0 ',修订:'4c2593c',时间:'2016-02-11 19:03:33'系统信息:主机:'rsn-GA-78LMT-S2',ip:'127.0.1.1',os.name:' Linux',os.arch:'amd64',os.version:'3.13.0-54-generic',java.version:'1.7.0_101'会话 ID:17716ecc-ffe3-40f9-92a6-8a106acf478dDriver 信息:org. openqa.selenium.firefox.FirefoxDriver 功能 [{platform=LINUX,acceptSslCerts=true,javascriptEnabled=true,cssSelectorsEnabled=true,databaseEnabled=true,browserName=firefox,handlesAlerts=true,nativeEvents=false,webStorageEnabled=true,rotatable=false,locationContextEnabled=true,pplicationCacheEnabled=true , takeScreenshot=true, version=38.0.1}] atun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

【问题讨论】:

  • 动作 c_history1 = new Actions(obj); WebElement visbilityhc = obj.findElement(By.xpath("//span[contains(text(),'Visibility')]")); WebElement vhistoryc = obj.findElement(By.xpath("//span[text() = 'History']")); WebElement vhcalls = obj.findElement(By.xpath("//span[(text() = 'Calls')]")); c_history1.moveToElement(visbilityhc).moveToElement(vhistoryc).moveToElement(vhcalls).click().build().perform();
  • 如果您在最初发布问题后想要添加一些额外的详细信息(如上面的代码),而不是将其添加为评论,请继续编辑您的问题并将其添加到那里它可以被正确格式化并且更容易被未来的读者找到。
  • 当您收到异常ElementNotVisibleException 时,这意味着您的元素已找到但不可见,因此无法与之交互。 Selenium 被设计为只与用户交互的元素,因为用户不能与不可见的元素交互,Selenium 也不会。从您的Actions 代码来看,您似乎必须悬停几次然后单击。您是否尝试过添加断点并单步执行代码以查看会发生什么?它可能会帮助您找到问题。

标签: selenium-webdriver


【解决方案1】:

如果您想获取具有文本Historya 元素,请尝试使用linkTextExpectedConditions.visibilityOfElementLocated 等到元素可见,如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement linkElement= wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("History")));

或者如果你想获得具有文本Historyspan 元素,请尝试使用xPath,如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement spanElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text() = 'History']")));

已编辑:- 如果此 element 通过鼠标悬停事件可见,请尝试使用 Action 执行 MouseOver,如下所示:-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement spanElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[text() = 'History']")));
Actions builder = new Actions(driver);    
builder.moveToElement(spanElement).perform();  

builder.mouse.mouseMove(((Locatable)spanElement).coordinates)  

希望它会起作用..:)

【讨论】:

  • 其实就是鼠标悬停在元素上,visibilit>History-->调用,所以我需要使用actions类,你能建议一下
  • Actions c_history1 = new Actions(obj);WebElement visbilityhc = obj.findElement(By.xpath("//span[contains(text(),'Visibility')]")); WebElement vhistoryc = obj.findElement(By.xpath("//span[text() = 'History']")); WebDriverWait 等待 = 新的 WebDriverWait(obj, 2); WebElement spanElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text() = 'Calls']"))); //WebElement vhcalls = obj.findElement(By.xpath("//span[contains(text(),'Calls')]")); c_history1.moveToElement(visbilityhc).moveToElement(vhistoryc).moveToElement(spanElement).click().build().perform();
  • 动作 c_history1 = new Actions(obj); WebElement visbilityhc = obj.findElement(By.xpath("//span[contains(text(),'Visibility')]")); WebElement vhistoryc = obj.findElement(By.xpath("//span[text() = 'History']")); WebElement vhcalls = obj.findElement(By.xpath("//span[(text() = 'Calls')]")); c_history1.moveToElement(visbilityhc).moveToElement(vhistoryc).moveToElement(vhcalls).click().build().perform();
  • @KumarUppu 你能分享你想要自动化的网站 URL,这就是为什么我可以给你更好的解决方案..
  • @KumarUppu 您提供的第二个代码看起来没问题...有什么问题吗??
猜你喜欢
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 2017-01-27
相关资源
最近更新 更多