'Clicking an element in Python using Selenium
I have been trying to get my code to click a button on a webpage to no avail. The button, when clicked is supposed to download some data to a CSV file. My code does not fail, it just doesn't seem to be clicking the button since nothing downloads.
Here is the full code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
PATH = "*File Path to Chromedriver*"
driver = webdriver.Chrome(PATH)
driver.get("https://api.gdeltproject.org/api/v2/summary/summary?d=web&t=summary")
driver.set_page_load_timeout(6)
try:
text ='(wildfire or wildfires or bushfire or brushfire or brushfires or bushfires)'
Keyword = driver.find_element_by_name("k")
Keyword.clear()
Keyword.send_keys(text)
Main_button = driver.find_element_by_xpath('//*[@id="submitform"]/div[3]/div[3]/div[5]/span[2]/div/button')
Main_button.click()
text_country = '//*[@id="submitform"]/div[3]/div[3]/div[5]/span[2]/div/ul/li[' + str(261) + ']/a/label/input'
Country_button = driver.find_element_by_xpath(text_country)
Country_button.click()
Submit_button = driver.find_element_by_xpath('//*[@id="submitform"]/div[5]/button')
Submit_button.click()
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "timelinemerge")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='highcharts-grba7qm-0']/div[2]/div[2]/button"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div[3]/div[3]/ul/li[7]"))).click()
driver.back()
except TimeoutException:
driver.back()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
