Search code examples
pythonseleniumselenium-webdriverselenium-chromedriverwebdriverwait

Click button with Selenium Python works only with debug mode


I tried to get data from the menu in this website. I've written this script which seems work well in debug mode. In this script, i close the chrome every time I've get information for certain city.

In debug mode, the detail information is shown when 'element' is clicked by the script. However, if i run the script, it seems that it doesn't do anything after city information is sent. The button 'Visualize Results' is enabled in the website when city data is entered, but the detail information that supposed to be shown after clicking this button by the script is not shown as if it is not clicked. Is there something that i miss?

thank you in advance.

for city in Cities:
    chrome_options = webdriver.ChromeOptions()   
    chrome_options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get(link)            
    driver.find_element(By.ID, "inputText").send_keys(city)    
    driver.find_element(By.ID, 'btninputText').click()
    element = wait(driver, 5).until(EC.presence_of_element_located((By.XPATH,div[@id="btviewPVGridGraph"]'))).click()
    driver.close()

Solution

    1. You need to wait for clickability of all 3 elements you accessing here. Presence is not enough.
    2. Need to add a short delay between clicking the search button and clicking visualization button

    The following code works:

    import time
    
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(options=options, service=webdriver_service)
    wait = WebDriverWait(driver, 30)
    
    url = "https://re.jrc.ec.europa.eu/pvg_tools/en/"
    driver.get(url)
    
    wait.until(EC.element_to_be_clickable((By.ID, "inputText"))).send_keys("Paris")
    wait.until(EC.element_to_be_clickable((By.ID, "btninputText"))).click()
    time.sleep(2)
    wait.until(EC.element_to_be_clickable((By.ID, "btviewPVGridGraph"))).click()
    

    Th result is:

    enter image description here