Twitter bootstrap 无法访问Selenium Webdriver中的引导模式对话框
我想访问打开的“模型”对话框的内容,并想访问按钮(是、否) 下面是HTML代码Twitter bootstrap 无法访问Selenium Webdriver中的引导模式对话框,twitter-bootstrap,selenium,Twitter Bootstrap,Selenium,我想访问打开的“模型”对话框的内容,并想访问按钮(是、否) 下面是HTML代码 <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"><div class="bootstrap-dialog-header"> <div class="bootstrap-dialog-close-button" style="di
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"><div class="bootstrap-dialog-header">
<div class="bootstrap-dialog-close-button" style="display: none;">
<button class="close">×</button>
</div>
<div class="bootstrap-dialog-title" id="e6adf6aa-dcbf-4fb8-9935-c083762f2812_title">
Inactivate user
</div>
</div>
</div>
<div class="modal-body">
<div class="bootstrap-dialog-body">
<div class="bootstrap-dialog-message">
Are you sure you want Inactivate user?
</div>
</div>
</div>
<div class="modal-footer">
<div class="bootstrap-dialog-footer">
<div class="bootstrap-dialog-footer-buttons">
<button class="btn btn-default" id="868d2d8a-67f6-4308-a3c8-0246a5d4618c">Yes</button>
<button class="btn btn-default" id="23e4d027-ef32-4b58-b8b6-6f95bead2db4">No</button>
</div>
</div>
</div>
×
停用用户
您确定要停用用户吗?
对
不
还有一件事是按钮的id是动态的。相反,我尝试使用xpath
比如:
By.XPath(//div[@class='bootstrap-dialog-footer-buttons']//按钮[text()='Yes']))
我希望这会有所帮助。在元素ID未预定义的情况下,CSS类可以用作有效的选择器。 当然,如果页面中只有一个元素具有该类,则这一点也适用。 对于多个元素,我建议您使用不同的css类标记每个元素,如:
<button class="yes btn btn-default" id="868d2d8a-67f6-4308-a3c8-0246a5d4618c">Yes</button>
<button class="no btn btn-default" id="23e4d027-ef32-4b58-b8b6-6f95bead2db4">No</button>
使用硒元素来达到所需的元素。在谷歌搜索了这么多之后,我找到了解决方案。。。这是一个简单的解决方案
//Switch to active element here in our case its model dialogue box.
driver.switchTo().activeElement();
Thread.sleep(3000);
// find the button which contains text "Yes" as we have dynamic id
driver.findElement(By.xpath("//button[contains(text(),'Yes')]")).click();
这段代码解决了我的问题。这解决了我的节点webdriver问题。driver.switchTo().activeElement()。然后(()=>{})谢谢!!
//Switch to active element here in our case its model dialogue box.
driver.switchTo().activeElement();
Thread.sleep(3000);
// find the button which contains text "Yes" as we have dynamic id
driver.findElement(By.xpath("//button[contains(text(),'Yes')]")).click();