Webscrap with Selenium - How to select content from a drop-down menu item with quotes in id

Asked

Viewed 317 times

0

I see you have a similar question, but the answer still hasn’t helped me.

I need to collect data from the Central Bank. For this, I need to download the files that are in a drop down, one at a time.

However, I’m already having trouble selecting just one.

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

PATH = "/usr/local/bin/chromedriver" # É preciso baixar o driver do navegador e por na pasta, devem ser a mesma versao
driver = webdriver.Chrome(PATH)

driver.get("https://www.bcb.gov.br/acessoinformacao/legado?url=https:%2F%2Fwww.bcb.gov.br%2Ffis%2Finfo%2Finstituicoes.asp%3Fidpai%3DINFCAD")
driver.implicitly_wait(15)

select = Select(driver.find_element_by_id('Cooperativas')).click()

select.select_by_visible_text('Maio/2019').click()

driver.quit()

It opens Chrome, enters the site, waits the 15 seconds for the site to load, but gives the following error:

Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="Cooperatives"]"}

On the site, I have the select as follows:

<select id="Cooperativas" "parametro"="" name="PARAMETRO"> Cooperativas
<option value="/fis/info/cad/cooperativas/202005COOPERATIVAS.zip">Maio/2020</option>
(outros options embaixo)

I tried to use find_element_by_xpath, but I got the same results.

1 answer

1


Your problem is occurring because all the information you are trying to select is within a frame, try as follows:

from selenium.webdriver.support.ui import Select
from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()

driver.get('https://www.bcb.gov.br/acessoinformacao/legado?url=https:%2F%2Fwww.bcb.gov.br%2Ffis%2Finfo%2Finstituicoes.asp%3Fidpai%3DINFCAD')
sleep(3)
frame = driver.find_element_by_name('framelegado')
driver.switch_to.frame(frame)
sleep(2)
select = Select(driver.find_element_by_id('Cooperativas'))
select.select_by_visible_text('Maio/2019')

Imagem Frame

  • Perfect! in the examples I saw, they went straight to select, they didn’t use the frame before. Thanks!

  • 1

    Glad it worked out, whenever you have a popup, window, alert or frame you need to use switch_to() before to be able to interact

  • Thanks for the tip!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.