【问题标题】:How can i select this button and click it?如何选择此按钮并单击它?
【发布时间】:2021-11-09 04:16:17
【问题描述】:

如何使用 selenium 从列表元素中选择此按钮并单击它? 这是html

<li class="ml1 sel">
    <a href="#__about.htm" id="about_page" onclick="return menuClick(this);" class="T sel">
        <span>about us</span>
    </a>
</li>

我尝试过使用xpathcss selectoridclass name 但总是报错selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:

请问有谁可以帮帮我!

【问题讨论】:

  • 检查Element 是否在iframe 中。如果没有,请发布您尝试过的代码。

标签: python-3.x selenium selenium-firefoxdriver


【解决方案1】:

您也许可以使用find_element_by_partial_link_textfind_element_by_link_text 函数。

尝试使用此链接了解如何使用它。 Link

【讨论】:

    【解决方案2】:

    在 Selenium 中有 4 种点击方式。

    我将使用这个 xpath

    //span[contains(text(), 'about us')]//parent::a[@id='about_page']
    

    代码试用 1:

    time.sleep(5)
    driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']").click()
    

    代码试用 2:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'about us')]//parent::a[@id='about_page']"))).click()
    

    代码试用 3:

    time.sleep(5)
    button = driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']")
    driver.execute_script("arguments[0].click();", button)
    

    代码试用 4:

    time.sleep(5)
    button = driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']")
    ActionChains(driver).move_to_element(button).click().perform()
    

    进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2014-04-09
      • 2017-08-02
      相关资源
      最近更新 更多