【问题标题】:Unable to click on button which is situate on popup made by <div> using Selenium WebDriver无法单击 <div> 使用 Selenium WebDriver 制作的弹出窗口上的按钮
【发布时间】:2016-09-08 08:35:39
【问题描述】:

HTML 代码

</div>
<div class="qx-outSet" qxselectable="off" style="overflow:...>
<div class="qx-button" tabindex="4" qxselectable="off" style="overflow:...>

******************************************** I have to click on bellow OK button ****************************
<div style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis; box-sizing: border-box; position: absolute; cursor: default; -moz-user-select: none; color: rgb(26, 26, 26); font-family: Arial,"Liberation Sans",sans-serif; font-size: 14px; left: 24px; top: 3px; width: 21px; height: 16px;" qxselectable="off" qxanonymous="true">OK</div>
</div>**

<div class="qx-button" tabindex="5" qxselectable="off" style="overflow: hidden; box-sizing: border-box; position: absolute; outline: medium none; -moz-user-select: none; cursor: default; padding: 3px 9px; left: 88px; top: 9px; width: 70px; height: 24px;">
<div style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis; box-sizing: border-box; position: absolute; cursor: default; -moz-user-select: none; color: rgb(26, 26, 26); font-family: Arial,"Liberation Sans",sans-serif; font-size: 14px; left: 12px; top: 3px; width: 45px; height: 16px;" qxselectable="off" qxanonymous="true">Cancel</div>
</div>

现在我正在使用 Selenium Web Driver 和 TestNG 来自动化在 Qooxdoo 框架(它是 javaScript 框架)中开发的 Web 应用程序。
在这我已经完成了一些事情,但是有一些弹出窗口是使用嵌套的&lt;div&gt; 和 selenium 弹出处理程序无法处理的。 通过使用操作类,我可以在弹出窗口中显示的文本区域{通过下面提到的硒代码}编写(查找附件),但我无法单击按钮(确定,取消)。

// wd is WebDriver Object 
WebElement element = wd.findElement(By.className("qx-window")); // qx-window is class name of div in which text area is  present
Actions actions = new Actions(wd);
actions.moveToElement(element).click().perform();
Thread.sleep(5000);
// write text in text area 
wd.findElement(By.tagName("textarea")).sendKeys("TEST");

wd.findElement(By.xpath(".//*[@id='demindoRoot']/div[4]/div[2]/div[2]/div[1]")).click();  //  CODE TO CLICK ON "OK" BUTTON 

我试图通过再次使用动作类将焦点切换到按钮所在的 div,但我也不能。

错误日志:

org.openqa.selenium.NoSuchElementException: 无法定位元素:{"method":"xpath","selector":"....

【问题讨论】:

  • 为目标按钮和异常日志提供 HTML 任何引发。还要告诉你是否尝试过使用简单的click() 方法来点击按钮
  • 您好 Andersson,我在问题描述中附加了整个 HTML 代码.....我已经尝试了简单的 click() 方法,但我得到了 2-3 个错误,即找不到元素或没有元素可点击.....
  • 您的问题中有很多信息,但对解决您的问题几乎没有帮助。使用简短的问题描述、HTML 目标按钮(+父元素)、Java 您尝试单击按钮的代码、异常日志来编辑您的问题。这将清楚并防止投票失败
  • 更新了描述.....
  • 你不应该像".//*[@id='demindoRoot']/div[4]/div[2]/div[2]/div[1]"那样使用绝对的XPathes,而是使用相对的,因为它们更可靠和灵活

标签: java selenium selenium-webdriver webdriver qooxdoo


【解决方案1】:

试试下面的代码,如果有任何异常,请告诉我:

wd.findElement(By.xpath("//div[text()='OK']")).click();

【讨论】:

  • 谢谢!!! Andersson .... 对于您对此的宝贵意见。它对我有用....我们可以使用 (By.xpath("//div[tabindex()='OK']")).click(); like (By.xpath("//div[text()='OK']")).click(); ?....
  • 如果要使用XPath中的属性,请使用//div[@tabindex="5"]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2017-12-18
相关资源
最近更新 更多