无法访问 Selenium webdriver java 中的 UI 对话框

Unable to access UI dialog in Selenium webdriver java

我有一个 iframe,然后是一个包含创建发票按钮的小部件。它不在 iframe 内部,但在它外部,我无法访问 it.I 无法使用 selenium webdriver java 访问此对话框,我正在尝试使用 java:[=14 访问创建发票按钮=]

</div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" id="saveBtnDialog" class="btn btn--primary">Create Invoice</button><button type="button" id="CancelBtnDialog" class="btn btn--primary">Cancel</button></div></div></div>  

我的代码:

 private final By createInvoiceBtn = By.id("saveBtnDialog");
 driver.switchTo().frame(driver.findElement(By.cssSelector(".ui-dialog-buttonset")));
 driver.findelement(createInvoiceBtn).click();

据我了解,如果您的 button 不在 frame 中,而您已经在 frame

您需要从frame切换到defaultContent然后去寻找element并执行如下操作:-

//first switch to out side the frame
driver.switchTo().defaultContent();

//now implement WebDriverWait to provide wait
WebDriverWait wait = new WebDriverWait(driver, 100);

//wait until ExpectedConditions is not true
WebElement buttonEl = wait.until(ExpectedConditions.elementToBeClickable(By.id("saveBtnDialog")));

//now you go for the action
buttonEl.click(); 

希望对您有所帮助...:)