Debug School

rakesh kumar
rakesh kumar

Posted on

How to apply checkbox dynamically using selenium webscrapping

References1
References2
References3

Method 1

To perform web scraping with dynamic checkboxes and clicking the "Show Results" button using Selenium in Python, you can follow these steps. In this example, we'll use the website "http://www.echoecho.com/htmlforms09.htm" for demonstration purposes.

Import the necessary libraries and set up the Selenium WebDriver:

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

# Initialize the WebDriver (make sure you have the appropriate WebDriver for your browser)
driver = webdriver.Chrome(executable_path="path_to_chromedriver")
driver.get("http://www.echoecho.com/htmlforms09.htm")
Enter fullscreen mode Exit fullscreen mode

Find and interact with the checkboxes. In this example, we'll select multiple checkboxes by their value attribute:

# Check multiple checkboxes based on their 'value' attribute
checkbox_values_to_select = ['Bike', 'Car', 'Bus']
for value in checkbox_values_to_select:
    checkbox = driver.find_element(By.XPATH, f'//input[@value="{value}"]')
    if not checkbox.is_selected():
        checkbox.click()
Enter fullscreen mode Exit fullscreen mode

Find and click the "Show Results" button:

# Find and click the "Show Results" button
show_results_button = driver.find_element(By.NAME, 'B1')
show_results_button.click()
Enter fullscreen mode Exit fullscreen mode

Wait for the results page to load and extract the data:

# Wait for the results page to load (you may need to adjust the wait time)
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, '//div[@id="body"]')))


# Extract and print the results
results = driver.find_element(By.XPATH, '//div[@id="body"]').text
print(results)
Enter fullscreen mode Exit fullscreen mode

Finally, close the WebDriver when you're done:

# Close the WebDriver
driver.quit()
Enter fullscreen mode Exit fullscreen mode

Make sure to replace "path_to_chromedriver" with the actual path to your Chrome WebDriver executable.

This code navigates to the provided webpage, selects checkboxes by their values, clicks the "Show Results" button, waits for the results page to load, extracts the results, and then closes the WebDriver.

Another Methods

To apply checkboxes dynamically and click the "Show Result" button using Selenium in Python, you can follow these steps based on your HTML source code:

<span class="filter_filter_option_label__v_iAW">
    <input class="styled-checkbox" id="filter_jFLoc_243" type="checkbox" name="location" readonly="" value="243">
    <label class="filter_filter_option_label__v_iAW" for="filter_jFLoc_243">Bangalore (1558)</label>
</span>
Enter fullscreen mode Exit fullscreen mode

Import the necessary libraries and set up the Selenium WebDriver:

from selenium import webdriver
from selenium.webdriver.common.by import By

# Initialize the WebDriver (make sure you have the appropriate WebDriver for your browser)
driver = webdriver.Chrome(executable_path="path_to_chromedriver")
driver.get("URL_OF_YOUR_PAGE")
Enter fullscreen mode Exit fullscreen mode

Locate and interact with the checkbox:

# Find the checkbox by its ID (replace "filter_jFLoc_243" with the actual ID)
checkbox = driver.find_element(By.ID, "filter_jFLoc_243")

# Check the checkbox if it's not already selected
if not checkbox.is_selected():
    checkbox.click()
Enter fullscreen mode Exit fullscreen mode

Find and click the "Show Result" button (you'll need to inspect the page source to identify the button element):

# Find and click the "Show Result" button by its attributes (e.g., class or text)
show_result_button = driver.find_element(By.XPATH, "//button[@class='your-button-class']")
show_result_button.click()
Enter fullscreen mode Exit fullscreen mode

Optionally, you can wait for the results to load (if there's a loading delay) and extract the data.

Finally, close the WebDriver when you're done:

# Close the WebDriver
driver.quit()
Enter fullscreen mode Exit fullscreen mode

Replace "path_to_chromedriver" with the actual path to your Chrome WebDriver executable and "URL_OF_YOUR_PAGE" with the URL of the web page you want to interact with.

Top comments (0)