我正在尝试使用 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/