python - 使用没有 ID 的 Selenium 查找并单击按钮

标签 python python-3.x selenium

我正在尝试使用 Python 中的 Selenium 单击网页上的特定按钮。如何使用以下 HTML 选择并单击“替换”按钮?

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-dialog-buttons ui-draggable ui-resizable no-close" tabindex="-1" role="dialog" style="position: absolute; height: auto; width: 300px; top: 411.009px; left: 692.001px; display: block;" aria-describedby="dialog" aria-labelledby="ui-id-5">
    <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
        <span id="ui-id-5" class="ui-dialog-title">Search and replace</span>
        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" aria-disabled="false" title="close">
            <span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span>
            <span class="ui-button-text">close</span>
        </button>
    </div>
    <div id="dialog" class="title ui-dialog-content ui-widget-content" style="width: auto; min-height: 62px; max-height: none; height: auto;">
        <div class="findReplaceText">Search for:</div>
        <input type="text" id="FindTerm" class="text ui-widget-content ui-corner-all input-large">
        <br>
        <div class="findReplaceText">Replace with:</div> 
        <input type="text" id="ReplaceTerm" class="text ui-widget-content ui-corner-all input-large"></div>
    <div class="ui-resizable-handle ui-resizable-n" style="z-index: 90;">
    </div>
    <div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div>
    <div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div>
    <div class="ui-resizable-handle ui-resizable-w" style="z-index: 90;"></div>
    <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div>
    <div class="ui-resizable-handle ui-resizable-sw" style="z-index: 90;"></div>
    <div class="ui-resizable-handle ui-resizable-ne" style="z-index: 90;"></div>
    <div class="ui-resizable-handle ui-resizable-nw" style="z-index: 90;"></div>
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
        <div class="ui-dialog-buttonset">
            <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
                <span class="ui-button-text">Replace</span>
            </button>
            <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
                <span class="ui-button-text">Cancel</span>
            </button>
        </div>
    </div>
</div>

HTML 中还有另一个应忽略的“替换”按钮实例:

<button type="button" class="titleButton replaceButton" id="riskFieldTemplateReplace">Replace</button>

更新:

不确定这是否有帮助。悬停/单击时按钮 HTML 如何更新。

将鼠标悬停在按钮上时:

<div class="ui-dialog-buttonset">
    <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover" role="button" aria-disabled="false">
        <span class="ui-button-text">Replace</span>
    </button>
</div>

点击按钮时:

<div class="ui-dialog-buttonset">
    <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover ui-state-focus ui-state-active" role="button" aria-disabled="false">
        <span class="ui-button-text">Replace</span>
    </button>
</div>

当我运行时:

driver.find_element_by_xpath(".//button[//span[text() = 'Replace' and @class = 'ui-button']]").click()

我看到以下内容:

Traceback (most recent call last):
  File "<pyshell#205>", line 1, in <module>
    driver.find_element_by_xpath(".//button[//span[text() = 'Replace' and @class = 'ui-button']]").click()
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 294, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 748, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 237, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//button[//span[text() = 'Replace' and @class = 'ui-button']]"}
  (Session info: chrome=52.0.2743.116)
  (Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 6.1.7601 SP1 x86_64)

最佳答案

您应该尝试使用xpath,如下所示:-

driver.find_element_by_xpath(".//button[contains(.,'Replace')]").click()

或者,如果有多个按钮具有相同的文本替换,请尝试如下:-

driver.find_element_by_xpath(".//button[child::span[text() = 'Replace' and @class = 'ui-button-text']]").click()

或者

driver.find_element_by_xpath(".//span[text() = 'Replace' and @class = 'ui-button-text']/parent::button").click()

已编辑:如果由于其他元素重叠而无法单击元素,您可以尝试使用单击 execute_script 如下:-

 replace = driver.find_element_by_xpath(".//span[text() = 'Replace' and @class = 'ui-button-text']/parent::button");

driver.execute_script("arguments[0].click()", replace); 

关于python - 使用没有 ID 的 Selenium 查找并单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977169/

相关文章:

python - 如何从平面图图像中检测门窗?

python - 这个 matplotlib.finance.candlestick2_ochl 代码会发生什么?

selenium - 如何从 docker 容器在网格上运行 selenium UI 测试?

python - 无法点击 selenium python 中带有链接的元素

Python:如何写入 fd 3?

javascript - 对 django View 的 Ajax 请求未返回响应

python - 每次点击 API 端点时如何加载 WTF 表单类

java - 如何避免 Firefox 中的窗口下载​​弹出窗口使用 Java selenium?我需要自动下载而不询问弹出窗口吗?

python - 保存带有日期变量扩展名的文件

Python 3.x - 在将项目附加到列表之前运行函数