Radio button selection - Selenium and Python

Asked

Viewed 415 times

0

Good morning, everyone!

I’m trying to extract information from the central bank website using Lenium with python. First let me explain that although there is an API to get the SELIC number, it only sends two decimal places, so it is not good for me.

The address is as follows:: https://www.bcb.gov.br/htms/selic/selicacumul.asp?frame=1

In him, what I need is:

  1. Select the radio button "Monthly";
  2. The "last" month in the Month and Year fields;
  3. Click on the "Query";
  4. Get the information that is displayed in "Accumulated Factor";

When trying to take the first step...

from selenium import webdriver

url = 'https://www.bcb.gov.br/htms/selic/selicacumul.asp?frame=1'
driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_xpath('/html/body/div[1]/div[1]/form/div[2]/div[5]/input').click()

I’ve also tried id and tried to wait for the shipment, but nothing worked.

Following error I get:

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    driver.find_element_by_xpath('/html/body/div[1]/div[1]/form/div[2]/div[5]/input').click()
  File "C:\Users\joaoluistr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\joaoluistr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\joaoluistr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\joaoluistr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[1]/form/div[2]/div[5]/input"}
  (Session info: chrome=83.0.4103.116)

Thanks in advance for the help, guys!

1 answer

1


By inspecting this page you can see that the content you need to access is within an Iframe, so if you try to access Selenium directly you will never be able to find, even with the full Xpath element

DevTools

So, you need to tell your Driver to enter the first Iframe, if there were more than one maybe specify which one you need

    driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
    driver.find_element_by_xpath("/html/body/div[1]/div[1]/form/div[2]/div[5]/input").click()
    dropbox = Select(driver.find_element_by_xpath("/html/body/div[1]/div[1]/form/div[2]/div[6]/select"))
    dropbox.select_by_visible_text('Janeiro')
    #para usar o dropbox adicione from selenium.webdriver.support.select import Select

Browser other questions tagged

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