简体   繁体   中英

Select RadioButton Selenium python

On the website : https://www.investing.com/economic-calendar/ I try to set some filters to collect data. I want to set the filter time on : "Display time only" and the filter importance on : 2 and 3 bulls. I tried the following script but none of the Radio button are interactable.

base_url = "https://www.investing.com/economic-calendar/"
driver = webdriver.Chrome(path, port=5007)
driver.get(base_url)

# expand filters
driver.find_element_by_id("filterStateAnchor").click()

Radio_bull2 = driver.find_element_by_xpath("//input[@id='importance2']")
Radio_bull2.click()

Radio_bull3 = driver.find_element_by_xpath("//input[@id='importance3']")
Radio_bull3.click()

Radio_time = driver.find_element_by_xpath("//input[@id='timetimeOnly']")
Radio_time.click()

I would greatly appreciate some help with this. Thank you!

To click on dynamic element like Display time only and bull2 & bull3 induce WebDriverWait and element_to_be_clickable()

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,'filterStateAnchor'))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,'timetimeOnly'))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,'importance2'))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,'importance3'))).click()

You need to add following libraries.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM