Italo Chipana Rocca
Italo Chipana Rocca

Reputation: 3

I want to click on a button with Selenium

I´m trying to web scraping and i need to simulate a click on a buttoN, I´ve tried this:

url = "https://apps5.mineco.gob.pe/transparencia/mensual/default.aspx?y=2021&ap=ActProy"
driver = driver = webdriver.Chrome()
driver.get(url)
nivelGob = driver.find_element_by_xpath('//*[@id="ctl00_CPH1_BtnTipoGobierno"]')
nivelGob.click()

and returns me this error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ctl00_CPH1_BtnTipoGobierno"]"} (Session info: chrome=88.0.4324.190)

I've been trying find the element by css selector, class name but nothing.

This is the button:

Nivel de gobierno

I hope someone can help me. Thanks a lot.

Upvotes: 0

Views: 77

Answers (2)

Skyler Schultze
Skyler Schultze

Reputation: 146

The website is actually within another frame so you need to switch to that frame. Try this:

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

url = "https://apps5.mineco.gob.pe/transparencia/mensual/default.aspx?y=2021&ap=ActProy"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(3)
frame = driver.find_element_by_id("frame0")
driver.switch_to.frame(frame)
w = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "ctl00_CPH1_BtnTipoGobierno"))).click()

Upvotes: 1

Tomasz
Tomasz

Reputation: 19

Perhaps the DOM isn't fully loaded yet. Try adding implicit wait to your driver

driver.implicitly_wait(10) # seconds

Upvotes: 0

Related Questions