-1

How can I send a click to the following elements using Selenium?

Note: They are placed at the same page and both of them have the same class "btn btn-primary"

<button class="btn btn-primary" data-ng-click="ctrl.findInstrumentsBySearch(ctrl.filterInstrument);" data-ng-disabled="ctrl.disableButtonSearchInstrument();">
    <span class="fa fa-search"></span> Pesquisar
</button>

<button class="btn btn-primary" data-ng-click="ctrl.downloadLimitInstrumentCsv(ctrl.filterInstrument,{ filename: &quot;export.csv&quot; });">
    <span class="fa fa-file-excel-o"></span> Exportar
</button>

When I try to use the following I receive the error "IndexError: list index out of range":

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

browser = webdriver.Chrome()
browser.get(("https://line.bvmfnet.com.br/#/limits/1"))
python_button = browser.find_elements_by_xpath("//button[@class='btn btn-primary' and @data-ng-click='ctrl.findInstrumentsBySearch(ctrl.filterInstrument)']")[0]
python_button.click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To click on the elements with text as save you can use either of the following Locator Strategies:

  • Pesquisar:

    • Using css_selector:

      driver.find_element_by_css_selector("button.btn.btn-primary[data-ng-click*='findInstrumentsBySearch'][data-ng-disabled*='disableButtonSearchInstrument']").click()
      
    • Using xpath:

      driver.find_element_by_xpath("//button[@class='btn btn-primary' and contains(@data-ng-click, 'findInstrumentsBySearch')][contains(., 'Pesquisar')]").click()
      

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Exportar:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary[data-ng-click*='downloadLimitInstrumentCsv']"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary' and contains(@data-ng-click, 'downloadLimitInstrumentCsv')][contains(., 'Exportar')]"))).click()
      
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352