Selenium – 无法单击模态内的元素
我正在使用Selenium和java,我无法点击模态中的元素。 场景是这样的:点击一个框架内的项目后,它打开一个模态,我需要点击这个模态内的一个元素,但我无法得到它。
我已经尝试过了:
js.executeScript("document.getElementById('saveexit').scrollIntoView(true);");
我也尝试过这样的switchTo():
while (itr.hasNext()) { String popup = itr.next(); System.out.println("itr: " + popup); driver.switchTo().window(popup); }
这是我的模态的html:
这是从firefox开发工具中获取的CSS路径:
html.no-touch body div.remodal-wrapper.remodal-is-opened div.modaliAdesione.remodal.remodal-is-initialized.remodal-is-opened div.modal-dialog div.modal-content.modal-custom-content div.modal-footer div.row.text-center div.col-md-6.col-sm-6.col-xs-12 button#saveexit.btn.modal-button.full-btn
永远找不到这个对象。
- 问题1:如果元素在模态内部必须以不同方式管理?
- 问题2:如何最终点击按钮saveexit工作?
这里共享了html的代码片段: https : //codeshare.io/arLW9q
这是java代码:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"saveexit\"]")))
我也尝试过:
cssSelector: #saveexit cssPath: html.no-touch body div.remodal-wrapper.remodal-is-opened div.modaliAdesione.remodal.remodal-is-initialized.remodal-is-opened div.modal-dialog div.modal-content.modal-custom-content div.modal-footer div.row.text-center div.col-md-6.col-sm-6.col-xs-12 button#saveexit.btn.modal-button.full-btn xpath: //*[@id="saveexit"]
请注意:如果我运行document.getElementById('saveexit').click();
它从浏览器的控制台开始运行
当您按照最佳实践使用Selenium-Java
客户端时,首要和最重要的试验必须是调用高效且经过validation的click()
方法。 根据click()
方法产生的错误,我们可以处理其他替代解决方案。
正如我从使用JavascriptExecutor
和switchTo().window()
的代码试验中看到的switchTo().window()
,您没有很好地识别出表示SAVE AND EXIT
按钮的WebElement
。
要单击SAVE AND EXIT
按钮,您可以使用以下代码块:
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='modal-dialog']//div[@class='modal-footer']//button[@class='btn modal-button full-btn' and @id='saveexit']"))).click();
我在我的脚本中使用jquery修复它;
这是行代码:
js.executeScript("$('#saveexit').trigger('click');");
我希望将来可以帮助别人。
我不知道为什么普通的javascript无法运行…