skip to Main Content

I’m learning how to use Selenium with Python, and as a simple exercise I’m trying to click a button on a page. I’ve successfully located the button and clicked it, and I see the button physically get clicked, and the new page loads for a split second (I can see the URL path change) and then it reloads the page I was originally on. I don’t run any other code after the button click.

Why is this happening, and how can I fix it?

I tried clicking the button using each of .click() and execute_script(). I also tried adding a call to time.sleep() after the button click.

code:

driver.get("website")
driver.implicitly_wait(5)

driver.find_element(By.XPATH, "//label[text()='some text']").click()
l = driver.find_element(By.XPATH, "//button[text()='Search']")
driver.execute_script("arguments[0].click();", l);

2

Answers


  1. This behavior might be due to JavaScript on the page or some other event handler interfering with the navigation. Here are some things you can try:

    Instead of using implicitly_wait, use WebDriverWait along with ExpectedConditions to wait for the desired element to be clickable:

        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.support import expected_conditions as EC
        from selenium.webdriver.common.by import By
        
        driver.get("website")
        
        wait = WebDriverWait(driver, 10)
        element = wait.until(EC.element_to_be_clickable((By.XPATH, "//label[text()='some text']")))
        element.click()
        
        search_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Search']")))
        search_button.click()
    

    or After clicking the button, wait for the URL to change or for an element to be present on the new page:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver.get("website")
    
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.element_to_be_clickable((By.XPATH, "//label[text()='some text']")))
    element.click()
    
    search_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Search']")))
    search_button.click()
    
    # Wait for the URL to change
    wait.until(EC.url_changes(driver.current_url))
    
    # Or wait for an element to be present on the new page
    # wait.until(EC.presence_of_element_located((By.XPATH, "//element_on_new_page")))
    
    Login or Signup to reply.
  2. The problem is with the site. You click "Search All" and the page refreshes but does nothing. There are some other weird things here as well where elements are available but if you click them too early, it doesn’t register.

    Steps:

    1. Navigate to the site and immediately click Search All. It doesn’t work but it will the next time it’s clicked but after all choices are set.
    2. After clicking Search All, we need to wait for the page to reset. We do that by waiting for the Search All button to go stale (means the element reference is now bad because the page reloaded).
    3. Set # of players
    4. Set course(s)
    5. Set date
      NOTE: date is set last because it opens a date picker that covers half the page. Setting the date last is simpler than having to deal with the date picker directly to have it close
    6. Click Search All (again)

    Now the search results page displays.

    Anyway, the code below is working.

    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.support.wait import WebDriverWait
    
    url = "https://cityofla.ezlinksgolf.com/index.html#/preSearch"
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get(url)
    
    wait = WebDriverWait(driver, 5)
    # search
    search = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[data-ng-click*='onSearchButtonClick']")))
    search.click()
    wait.until(EC.staleness_of(search))
    # set # of people
    Select(wait.until(EC.visibility_of_element_located((By.ID, "pc")))).select_by_visible_text("2")
    # choose courses
    driver.find_element(By.XPATH, "//ul[@class='automationPadding']//label[text()='Griffith Park - Harding']").click()
    # choose date
    date = wait.until(EC.element_to_be_clickable((By.ID, "dateInput")))
    date.clear()
    date.send_keys("04/15/2023")
    # search
    driver.find_element(By.CSS_SELECTOR, "[data-ng-click*='onSearchButtonClick']").click()
    
    # driver.quit()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search