DOM 中有 2 个类似的按钮(我们称它们为“安装”按钮),但我需要使用仅适用于该按钮所在的 html 块的文本来选择该按钮。
这是我使用的代码,下面是我得到的输出。
disable_operation_button = driver.find_element(By.XPATH, "//span[contains(text(), 'Disable Operation Re-executing')]")
parent_element_disable_button = disable_operation_button.find_element(By.XPATH, "../../../..")
print(parent_element_disable_button.get_attribute("outerHTML"))
print("**************isdisplayed")
# Printing whether the Install button is displayed
print(parent_element_disable_button.find_element(By.XPATH, "//button[@class='ant-btn ant-btn-primary']//span[contains(text(), 'Install')]").is_displayed())
parent_element_disable_button.find_element(By.XPATH, "//button[@class='ant-btn ant-btn-primary']//span[contains(text(), 'Install')]").click()
parent_element_disable_button 的输出如下(请注意,在最后 2 行中,存在 Install 按钮,
<div style="text-align: right;">
<div style="text-align: right; display: flex; flex-direction: row-reverse;">
<div style="margin: 8px 0px 8px 8px;">
<label class="ant-checkbox-wrapper">
<span class="ant-checkbox"><input type="checkbox" class="ant-checkbox-input" value=""><span class="ant-checkbox-inner"></span></span>
<span>
Disable Operation Re-executing
<span role="img" aria-label="question-circle" class="anticon anticon-question-circle">
<svg viewBox="64 64 896 896" focusable="false" data-icon="question-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true">
<path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z"></path>
<path d="M623.6 316.7C593.6 290.4 554 276 512 276s-81.6 14.5-111.6 40.7C369.2 344 352 380.7 352 420v7.6c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V420c0-44.1 43.1-80 96-80s96 35.9 96 80c0 31.1-22 59.6-56.1 72.7-21.2 8.1-39.2 22.3-52.1 40.9-13.1 19-19.9 41.8-19.9 64.9V620c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8v-22.7a48.3 48.3 0 0130.9-44.8c59-22.7 97.1-74.7 97.1-132.5.1-39.3-17.1-76-48.3-103.3zM472 732a40 40 0 1080 0 40 40 0 10-80 0z"></path>
</svg>
</span>
</span>
</label>
</div>
<div style="margin: 8px 0px 8px 8px;"><label class="ant-checkbox-wrapper"><span class="ant-checkbox"><input type="checkbox" class="ant-checkbox-input" value=""><span class="ant-checkbox-inner"></span></span><span>Schedule Install</span></label></div>
<div style="margin: 8px;"><label class="ant-checkbox-wrapper"><span class="ant-checkbox"><input type="checkbox" class="ant-checkbox-input" value=""><span class="ant-checkbox-inner"></span></span><span>Block Uninstall</span></label></div>
</div>
<span style="display: none;">
<div class="ant-picker">
<div class="ant-picker-input">
<input readonly="" placeholder="Select Time" title="" size="18" autocomplete="off" value="">
<span class="ant-picker-suffix">
<span role="img" aria-label="calendar" class="anticon anticon-calendar">
<svg viewBox="64 64 896 896" focusable="false" data-icon="calendar" width="1em" height="1em" fill="currentColor" aria-hidden="true">
<path d="M880 184H712v-64c0-4.4-3.6-8-8-8h-56c-4.4 0-8 3.6-8 8v64H384v-64c0-4.4-3.6-8-8-8h-56c-4.4 0-8 3.6-8 8v64H144c-17.7 0-32 14.3-32 32v664c0 17.7 14.3 32 32 32h736c17.7 0 32-14.3 32-32V216c0-17.7-14.3-32-32-32zm-40 656H184V460h656v380zM184 392V256h128v48c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8v-48h256v48c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8v-48h128v136H184z"></path>
</svg>
</span>
</span>
</div>
</div>
</span>
<button type="button" class="ant-btn ant-btn-primary" style="margin: 5px;"><span>Install</span></button>
</div>
对于显示 DOM 中是否存在安装按钮元素的输出,它显示 False。
**************isdisplayed
False
单击安装按钮 (
parent_element_disable_button.find_element(By.XPATH, "//button[@class='ant-btn ant-btn-primary']//span[contains(text(), 'Install')]").click()
) 时,会引发以下错误。
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
您能否建议一种解决此问题的方法或选择该按钮?
可能建议更改 isDisplayed 检查 -> ExpectedCondition。 elementToBeClickable(By locator) + 在单击此按钮之前添加一些等待。
element = WebDriverWait(driver, 20).until(
expected_conditions.element_to_be_clickable((By.XPATH, "xpath")))
还有机会并且可能有帮助:
无论如何,我想除了任何额外的技巧之外,等待应该对你有所帮助。