Home > Enterprise >  Unable to locate element (button) in Selenium with Python
Unable to locate element (button) in Selenium with Python

Time:03-02

Selenium does not find the button "Consultar". I already tried copying and finding by xpath, id, partial_text, and text.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select

url = 'https://www.anbima.com.br/pt_br/informar/sistema-reune.htm'
driver = webdriver.Chrome(executable_path= r"/Users/Test/chromedriver")
driver.get(url)

Information about the button :

<img src="../img/bt_consultar.gif" name="Consultar" onclick="VerificaSubmit()" style="cursor: pointer; cursor:pointer;">

CodePudding user response:

The Consultar button element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using css_selector:

      driver.get('https://www.anbima.com.br/pt_br/informar/sistema-reune.htm')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#LGPD_ANBIMA_global_sites__text__btn"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='https://www.anbima.com.br/informacoes/reune/default.asp']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img[name='Consultar'][src*='bt_consultar']"))).click()
      
    • Using xpath:

      driver.get('https://www.anbima.com.br/pt_br/informar/sistema-reune.htm')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='LGPD_ANBIMA_global_sites__text__btn']"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://www.anbima.com.br/informacoes/reune/default.asp']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[@name='Consultar' and contains(@src, 'bt_consultar')]"))).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
    
  • Related