【问题标题】:Click button by text using Python and Selenium使用 Python 和 Selenium 按文本单击按钮
【发布时间】:2016-05-29 22:51:20
【问题描述】:

是否可以单击与Selenium 相同文本的多个按钮?

【问题讨论】:

    标签: python selenium selenium-webdriver xpath webdriverwait


    【解决方案1】:

    您可以通过文本找到所有按钮,然后在for 循环中为每个按钮执行click() 方法。

    使用这个 SO answer 会是这样的:

    buttons = driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
    
    for btn in buttons:
        btn.click()
    

    我还建议您查看 Splinter,它是 Selenium 的一个很好的包装器。

    Splinter 是现有浏览器自动化之上的抽象层 Selenium、PhantomJS 和 zope.testbrowser 等工具。它有一个 使编写 Web 自动化测试变得容易的高级 API 应用程序。

    【讨论】:

    • 嗯,我必须点击大约 100 个不同的按钮,它们都有不同的 xpathas
    • 但它们的文字相同/看起来相同
    • 在我给出的示例中,您不是在寻找给定的 absolute xpath,而是在寻找按钮包含的文本。
    • driver.find_elements_by_xpath("//*[contains(text(), 'Unlock this result here')]").click() 不工作,我不明白?
    • 您能否向我们提供您尝试与之互动的网站的链接?或者向我们展示按钮的 html 代码?
    【解决方案2】:

    我在 html 中有以下内容:

    driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()
    

    【讨论】:

      【解决方案3】:

      要通过文本定位并单击 <button> 元素,您可以使用以下任一 Locator Strategies

      • 使用 xpathtext():

        driver.find_element_by_xpath("//button[text()='button_text']").click()
        
      • 使用 xpathcontains():

        driver.find_element_by_xpath("//button[contains(., 'button_text')]").click()
        

      理想情况下,要通过文本定位并单击<button> 元素,您需要为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下Locator Strategies 之一:

      • 使用 XPATHtext():

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='button_text']"))).click()
        
      • 使用 XPATHcontains():

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'button_text')]"))).click()
        
      • 注意:您必须添加以下导入:

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

      更新

      要通过文本定位所有<button> 元素,您可以使用以下任一Locator Strategies

      • 使用 xpathtext():

        for button in driver.find_elements_by_xpath("//button[text()='button_text']"):
          button.click()
        
      • 使用 xpathcontains():

        for button in driver.find_elements_by_xpath("//button[contains(., 'button_text')]"):
          button.click()
        

      理想情况下,要通过文本定位所有<button> 元素,您需要为visibility_of_all_elements_located() 诱导WebDriverWait,您可以使用以下Locator Strategies 之一:

      • 使用 XPATHtext():

        for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[text()='button_text']"))):
          button.click()
        
      • 使用 XPATHcontains():

        for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[contains(., 'button_text')]"))):
          button.click()
        
      • 注意:您必须添加以下导入:

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

      【讨论】:

        【解决方案4】:

        @nobodyskiddy,尝试使用driver.find_element(如果你有一个按钮选项),当你使用driver.find_elements时,使用click()的索引,find_elements将返回数组到web元素值,所以你有使用索引来选择或单击。

        【讨论】:

          猜你喜欢
          • 2018-08-22
          • 2021-01-31
          • 2020-08-06
          • 2017-04-22
          • 1970-01-01
          • 2013-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多