Home > Blockchain >  Failed to click on a button available on a webpage using selenium
Failed to click on a button available on a webpage using selenium

Time:02-15

I've been trying to figure out any way to click on a button visible as Download base consolidada on this webpage. The button appears to be within an iframe. It seems I've been able to switch to that iframe and locate the element of that button using some xpath that I've defined within the following script. However, the script throws some error when it comes to click on that button.

I'm trying with:

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

link = "https://www.anbima.com.br/pt_br/autorregular/matriz-de-probabilidade-de-resgates.htm"

with webdriver.Chrome() as driver:
  wait = WebDriverWait(driver,20)
  driver.get(link)
  wait.until(EC.frame_to_be_available_and_switch_to_it(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "iframe")))))
  wait.until(EC.element_to_be_clickable((By.XPATH,"//span[@class='textRun'][.='Download base consolidada']"))).click()

This is the error I get:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <span  style="font-size: 14pt; font-weight: bold; color: rgb(0, 135, 202);">...</span> is not clickable at point (345, 90). Other element would receive the click: <rect x="4.02685069008783" y="1.961104140526976" width="244.63136762860728" height="42.18820577164366" rx="8" ry="8" style="vector-effect: non-scaling-stroke; stroke-width: 3px; stroke: rgb(0, 149, 217); stroke-opacity: 1; fill: rgb(243, 242, 241); fill-opacity: 0;"></rect>
  (Session info: chrome=98.0.4758.102)
Stacktrace:
Backtrace:
    Ordinal0 [0x00307AC3 2587331]
    Ordinal0 [0x0029ADD1 2141649]
    Ordinal0 [0x00193BB8 1063864]
    Ordinal0 [0x001C65FF 1271295]
    and so on--------------------

How can I click on the aforementioned button using selenium?

CodePudding user response:

driver.get('https://www.anbima.com.br/pt_br/autorregular/matriz-de-probabilidade-de-resgates.htm')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "iframe")))))
WebDriverWait(driver, 40).until(EC.visibility_of_element_located((By.XPATH, "(//*[@data-automation-type='visualContainerHost']//div[contains(@aria-label, 'Web URL')])[1]"))).click()
time.sleep(5)
driver.quit()

This gives me the output:

Process finished with exit code 0 which implies that no errors and the code ran successfully.

Here is the snapshot of the file downloaded through the code: Excel file snapshot

CodePudding user response:

It's because the element you're trying to click isn't the one accepting the actual click but the rect element instead. Try replacing the Xpath of the span by the div containing the rect element and it should work.

wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@aria-describedby='visualsEnterHint-e780d1b86be1529ccdf0']/div[3]/div/visual-modern/div/div"))).click()
  • Related