python - Selenium - 无法使用 Python 单击按钮

标签 python selenium

我在尝试使用 Selenium 和 Python 单击 Web 应用程序上的特定按钮时遇到问题。该按钮显示在一个对话框中,按下另一个按钮后该对话框就可用。

例如,我的表单包含以下部分:

enter image description here

按下“保存答案”按钮后,上方的对话框中会显示“确定”按钮。

enter image description here

我正在尝试单击此“确定”按钮,但它现在不起作用。 当我在 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/

相关文章:

python3.7 aiohttp 很慢

python - 为什么我没有输出?

java - 使用 selenium xpath 动态查找前一个兄弟元素

python - 你如何在 Python 中的 Selenium Webdriver 上设置脚本超时?

python - 如何创建颜色和标记的图例?

Python 3.5 urllib 无法在浏览器中打开网页

ruby - 使用 watir-webdriver 打开多个线程导致 'Connection refused' 错误

python - 写入 JSON - 将\u00a3 转换为 £

java - 如何更改 TestNG 报告中的异常消息

python - 如何将 pandas DataFrame 中的 2 行与连续时间戳合并?