代码之家  ›  专栏  ›  技术社区  ›  Kai

如何在python selenium中单击特定div中的按钮

  •  0
  • Kai  · 技术社区  · 2 年前
    <div class="account-container loggedIn svelte-2hymnw"><button type="button" class="text-center font-medium focus-within:ring-4 focus-within:outline-none inline-flex items-center justify-center px-5 py-2.5 text-sm rounded-lg icon-btn"><span class="sr-only">Account</span> <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="shrink-0 w-4 h-4 nav-icon" role="img" aria-label="user solid" viewBox="0 0 14 18"><path fill="currentColor" d="M7 9a4.5 4.5 0 1 0 0-9 4.5 4.5 0 0 0 0 9Zm2 1H5a5.006 5.006 0 0 0-5 5v2a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2a5.006 5.006 0 0 0-5-5Z"></path></svg></button></div>
    

    这是div中的按钮。它没有特定的标识符。

    我已经尝试过了:

    driver.find_element(By.XPATH,"//div[@class='account-container.loggedIn']/button[1]").click()
    

    中的跨度有一个文本,可以找到,但无法单击。

    有人能帮我吗?

    TIA

    2 回复  |  直到 2 年前
        1
  •  1
  •   Muhammed Samed Özmen    2 年前

    你也可以用它。更好地使用 CSS_SELECTOR .

    driver.find_element(By.CSS_SELECTOR,".account-container .loggedIn .svelte-2hymnw button")
    
        2
  •  0
  •   seadawy    2 年前

    建议从浏览器中的检查工具复制XPath 检查 copy XPath from browser

    更好的代码格式是

    try:
        button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH,"//div[@class='account-container.loggedIn']/button[1]")))
        button.click()
    finally:
        driver.quit()