Home > Blockchain >  Selenium - dropdown menu
Selenium - dropdown menu

Time:06-12

I'm trying to scrape spreadsheets generated in this website: https://sisaps.saude.gov.br/sisvan/relatoriopublico/index

First you need to click on "Estado nutricional -> Selecionar relatório" on the top, then select a year at "Ano de Referência:", but I can't find an ID for this dropdown menu. I have tried this:

driver.find_element(By.ID, 'nuAno').select_by_value('2009')

But I get this error:

'WebElement' object has no attribute 'select_by_value'.

How to find it? Thanks!

CodePudding user response:

Make sure the element is located:

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

element = WebDriverWait(self.driver, 50).until(
                EC.presence_of_element_located((By.ID, "nuAno")))

Also make sure you are using Select:

from selenium.webdriver.support.ui import Select

Select(element).select_by_value("2009")

CodePudding user response:

I think it is because the element is not loaded. Try to interact with a button to load it.

CodePudding user response:

I used the SeleniumBase Recorder to generate the script:

from seleniumbase import BaseCase

class RecorderTests(BaseCase):
    def test_recording(self):
        self.open("https://sisaps.saude.gov.br/sisvan/relatoriopublico/index")
        self.click('strong:contains("SELECIONAR RELATÓRIO")')
        self.click('button[title="-SELECIONE-"]')
        self.select_option_by_text("select#nuAno", "2019")

There's also the Portuguese version:

from seleniumbase.translate.portuguese import CasoDeTeste

class RecorderTests(CasoDeTeste):
    def test_recording(self):
        self.abrir("https://sisaps.saude.gov.br/sisvan/relatoriopublico/index")
        self.clique('strong:contains("SELECIONAR RELATÓRIO")')
        self.clique('button[title="-SELECIONE-"]')
        self.selecionar_opção_por_texto("select#nuAno", "2019")

With SeleniumBase installed, you can run the scripts with pytest. Add the --demo option to slow down the tests so that you can see what's happening. (Full disclosure: I built the SeleniumBase framework.)

  • Related