Change date in Datepicker with Selenium/Python

640 views Asked by At

I am trying to acces and change the date on the following website with Python/Selenium:

http://www.b3.com.br/en_us/market-data-and-indices/data-services/market-data/historical-data/derivatives/trading-session-settlements/

When trying to click on the calender i get the following error: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/div[1]/div/form/div/div[1]

I guess i need to active some js-code but i am having trouble to locate the specific js-code. Does anyone have any suggestion to how i can activate the content on the webpage?

I have tried using the following code:

driver.get('http://www.b3.com.br/en_us/market-data-and-indices/data-services/market-data/historical-data/derivatives/trading-session-settlements/')
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/form/div').click()
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/form/div/div[1]').click()
driver.find_element_by_xpath('//*[@id="dData1"]').click()
driver.find_element_by_xpath('//*[@id="dData1"]').clear()
driver.find_element_by_xpath('//*[@id="dData1"]').send_keys('04/08/2020')

I get that the code already fails at line 2, but i dont understand why as i copied the Xpath like i always do, when using selenium on a webpage.

Thanks in advance for the help!

1

There are 1 answers

1
SeleniumUser On BEST ANSWER

iframe is present on your web page, switch control on it before performing send key. Refer below solution :

driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("http://www.b3.com.br/en_us/market-data-and-indices/data-services/market-data/historical-data/derivatives/trading-session-settlements/")
# driver.find_element_by_tag_name('body').send_keys("Keys.ESCAPE")
iframe=driver.find_element_by_id("bvmf_iframe")
driver.switch_to.frame(iframe)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#dData1.datepicker.hasdatepicker"))).clear()
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input#dData1.datepicker.hasdatepicker"))).send_keys('02/01/2021')

Note : Add below imports to your solution :

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

Output:

enter image description here