我正在使用 Selenium Web 驱动程序 3.0,并且想要从打开的两个对话框(一个在后台,第二个在前台)的 Activity 对话框中单击“确定”按钮。如何从 html 下面的父 div 单击前台对话框“确定”按钮?我尝试使用 nth-child 和 nth-of-type 但单击总是会发现第一个对话框在后台显示,并且 Web 驱动程序无法单击“确定”按钮。
当我检查 we.isDisplayed() 时,它也找到第一个“确定”按钮,我想要用于第二个对话框“确定”按钮的 we.isDisplayed() 方法。
HTML
注意:对话框 div id 可以是任何内容,但类名是固定的:DwtDialog。
尝试过的代码:
WebDriver webDriver;
WebElement we = webDriver.findElement(By.cssSelector("div[class='DwtDialog']:nth-child(2) td[id$='_button2_title']:contains('OK')"));
visible = we.click();
// Click fails here
尝试过的定位器:
By.cssSelector("div[class='DwtDialog']:nth-child(2) td[id$='_button2_title']:contains('OK')")
By.xpath("//div[@class='DwtDialog'][2]//td[@id='ErrorDialog_button2_title' and contains(text(), 'OK')]")
By.xpath("//div[@id='z_shell']//div[@class='DwtDialog'][2]//td[@id='ErrorDialog_button2_title' and contains(text(), 'OK')]")
câu hỏi
如何单击可见对话框的“确定”按钮?大多数情况下,此对话框是稍后加载的。我的意思是 nth-child(2)
和第三个对话框 nth-child(3)
作为提示。
编辑:我错过了有关 id 更改的评论...尝试#2。我认为这样的东西应该可以工作,但如果没有页面我无法测试它。基本上,我们抓取了对话框 div.DwtDialog
上的所有按钮 td.ZWidgetTitle
。如果它可见并且包含“确定”,请单击它。
List dialogButtons = driver.findElements(By.cssSelector("div.DwtDialog > td.ZWidgetTitle"));
for (WebElement dialogButton : dialogButtons)
{
if (dialogButton.isDisplayed() && dialogButton.getText().equals("OK"))
{
dialogButton.click();
phá vỡ;
}
}
编辑2:
获得更多信息后,这是另一种方法。如果不访问该网站,很难确定这些东西是否可以工作,但如果它不起作用,这希望能为您指明正确的方向。这将获得错误对话框上的所有“确定”按钮。问题是,哪一个是可点击的?我们可以吃掉另一个元素收到点击时抛出的异常,直到我们找到一个不抛出的异常......那就是正确的。我做了一些本地测试,这段代码似乎对我有用。
List dialogButtons = driver.findElements(By.xpath("//td[starts-with(@id, 'ErrorDialog_button') and text()='OK']"));
System.out.println(dialogButtons.size());
System.out.println(dialogButtons.size());
for (WebElement dialogButton : dialogButtons)
{
thử
{
dialogButton.click();
}
catch (WebDriverException e)
{
// không làm gì cả
}
}
Tôi là một lập trình viên xuất sắc, rất giỏi!