我有下面的 HTML。
<div class="interaction-container">
<button aria-busy="false" aria-disabled="false" aria-label="Selecionar feição" aria-
pressed="false" class="button button--text-visible" id="calcite-action-5c67ed3f-5f82-3432-28e7-
5fcb4568c10e-button">
<div aria-hidden="true" class="icon-container">
<calcite-icon class="" aria-hidden="true" icon="list" scale="s">
</calcite-icon><div class="slot-container">
<slot></slot></div></div><div class="text-container text-container--visible">1 de
2</div>
</button><slot name="tooltip"></slot><div aria-labelledby="calcite-action-5c67ed3f-5f82-3432-28e7-5fcb4568c10e-button" aria-live="polite" class="indicator-text" id="calcite-action-5c67ed3f-5f82-3432-28e7-5fcb4568c10e-indicator" role="region"></div></div>
我希望机器人能够简单地找到此按钮标签
我通过输入下面的代码解决了这个问题:
# Access the Shadow DOM and try to find the "Select feature" button
try:
print("Waiting for the 'Select feature' button to appear...")
calcite_action = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "calcite-action[title='Selecionar feição']"))
)
shadow_root = expand_shadow_element(driver, calcite_action)
select_feature_button = shadow_root.find_element(By.CSS_SELECTOR, "button.button--text-visible")
print("Button 'Select feature' found. Attempting to click...")
action.move_to_element(select_feature_button).perform()
time.sleep(1) # Small wait to simulate human interaction
action.click(select_feature_button).perform()
print("Button 'Select feature' clicked successfully!")
except Exception as e:
print(f"Error clicking the 'Select feature' button: {
e}")