【问题标题】:How to interact with button based on text using Selenium and Python如何使用 Selenium 和 Python 与基于文本的按钮进行交互
【发布时间】:2021-11-07 16:17:21
【问题描述】:

我正在尝试根据其文本单击按钮(使用 selenium)。例如,对于以下 HTML 标记:

<span class="MuiTab-wrapper jss483">Options</span>

我试过了:

driver.find_elements_by_xpath("//*[contains(text(), 'Options')]").click()

但是,Selenium 找不到任何带有以下文本的元素:“Options”。

有什么想法吗?

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

    你应该检查的xpath:

    //*[contains(text(), 'Options')]
    

    //span[contains(text(), 'Options')]
    

    检查步骤:

    Press F12 in Chrome -> 转到element 部分 -> 执行CTRL + F -> 然后粘贴xpath 看看,如果您想要的element 用@ 得到突出显示 987654330@匹配节点。

    在 Selenium 中有 4 种点击方式。

    代码试用 1:

    time.sleep(5)
    driver.find_element_by_xpath("//span[contains(text(), 'Options')]").click()
    

    代码试用 2:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'Options')]"))).click()
    

    代码试用 3:

    time.sleep(5)
    button = driver.find_element_by_xpath("//span[contains(text(), 'Options')]")
    driver.execute_script("arguments[0].click();", button)
    

    代码试用 4:

    time.sleep(5)
    button = driver.find_element_by_xpath("//span[contains(text(), 'Options')]")
    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
    

    另外,find_elements 返回一个列表,将其更改为 find_element 你应该可以继续。

    或者使用列表索引来指向网页元素。

    elems = driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")
    elems[0].click()
    

    不推荐这种方式。

    【讨论】:

      【解决方案2】:

      您正在使用find_elements_by_xpath 方法。它将为您提供 Web 元素列表,而不是单个 Web 元素。所以你不能在结果上应用.click()

      driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")
      

      您应该改用find_element_by_xpath,这样您的代码将是

      driver.find_element_by_xpath("//*[contains(text(), 'Options')]").click()
      

      或单击来自 web 元素列表返回器的第一个结果(或任何其他结果),如下所示

      driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")[0].click()
      

      您还可能需要添加一些延迟/等待,以便在访问元素之前加载它。
      或者,您可能需要将该元素滚动到视图中。
      或者该元素可能在 iframe 内...

      【讨论】:

        【解决方案3】:

        要单击文本为 Options 的元素,您可以使用以下任一Locator Strategies

        • 使用xpath

          driver.find_element(By.XPATH, "//span[contains(@class, 'MuiTab-wrapper') and contains(., 'Options')]").click()
          

        所需元素是启用JavaScript 的元素,因此理想情况下,单击需要为element_to_be_clickable() 诱导WebDriverWait 的元素,您可以使用以下Locator Strategies 之一:

        • 使用XPATH

          WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(@class, 'MuiTab-wrapper') and contains(., 'Options')]"))).click()
          
        • 注意:您必须添加以下导入:

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

        【讨论】:

          猜你喜欢
          • 2020-10-21
          • 2021-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 1970-01-01
          • 1970-01-01
          • 2016-05-29
          相关资源
          最近更新 更多