Python 与 Selenium - 无法找到特定元素

问题描述 投票:0回答:1

我有下面的 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>

我希望机器人能够简单地找到此按钮标签

javascript python selenium-webdriver
1个回答
0
投票

我通过输入下面的代码解决了这个问题:

# 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}")
© www.soinside.com 2019 - 2024. All rights reserved.