我在尝试使用 Selenium 和 Python 单击 Web 应用程序上的特定按钮时遇到问题。该按钮显示在一个对话框中,按下另一个按钮后该对话框就可用。
例如,我的表单包含以下部分:
按下“保存答案”按钮后,上方的对话框中会显示“确定”按钮。
我正在尝试单击此“确定”按钮,但它现在不起作用。 当我在 Chrome 上检查此元素时,我得到以下项目:
<div class="bootbox modal fade my-modal in" tabindex="-1" role="dialog" style="display: block;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="bootbox-body">Your answers have been saved.</div>
</div>
<div class="modal-footer">
<button data-bb-handler="cancel" type="button" class="btn btn btn-primary">OK</button>
</div></div></div></div>
与 <button data-bb-handler="cancel" type="button" class="btn btn btn-primary">OK</button>
是我试图点击的按钮。
我正在使用以下 Python 代码来尝试此操作:
driver.find_element_by_id("saveQuestionsSection").click() #click Save Answer
driver.find_element_by_xpath("//button[@type='button']").click() #click OK
但是它不起作用。我尝试使用 Selenium IDE 复制此步骤,它在那里工作,命令如下所示:
命令:单击
目标:xpath=(//button[@type='button'])[26]
但我不知道如何将其转换为 Python 代码。
有什么想法吗?
最佳答案
使用 CSS 选择器。
find_element_by_css_selector(".btn").click()
关于python - Selenium - 无法使用 Python 单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50373848/