I am trying to do the following exercise: open this page on Python, and check what happens when the "Remote eligible" button is on; are there positions displayed or not?
So far, I have this code but is giving me this error: Could not find or click ‘Remote eligible’ button: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="c1853"]"}
When the button is off the CSS selector is:
<input class="VfPpkd-muHVFf-bMcfAe" type="checkbox" id="c473" jsname="YPqjbf" jsaction="focus:AHmuwe; blur:O22p3e;change:WPi0i;" data-indeterminate="false">
When it is on, it is:
<input class="VfPpkd-muHVFf-bMcfAe" type="checkbox" id="c556" jsname="YPqjbf" jsaction="focus:AHmuwe; blur:O22p3e;change:WPi0i;" data-indeterminate="false" checked="">
How can I enhance my code?
# !apt-get update
# !apt install chromium-chromedriver
# !pip install selenium
# Import necessary libraries
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# Configure Selenium to use headless Chrome
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.binary_location = "/usr/bin/chromium-browser"
# Initialize WebDriver with options
driver = webdriver.Chrome(options=chrome_options)
# Open the webpage
driver.get("https://www.google.com/about/careers/applications/jobs/results?location=Chile&location=Santiago%2C%20Chile")
# Wait for the page elements to load
time.sleep(5) # Adjust the sleep time based on your network speed
# Attempt to locate and click the "Remote eligible" button if it's present
# Note: You'll need to adjust the selectors based on the actual page structure
try:
# Locate the checkbox element
checkbox_element = driver.find_element(By.ID, 'c1853')
# Check the value of the 'checked' attribute to determine if the button is on or off
is_button_on = checkbox_element.get_attribute('checked')
# Click the button only if it's off
if not is_button_on:
checkbox_element.click()
print("Clicked 'Remote eligible' to turn it on.")
time.sleep(3) # Wait for the page to update after clicking
except Exception as e:
print(f"Could not find or click 'Remote eligible' button: {e}")
# Now check if there are no positions displayed
# Adjust the method of checking based on how the webpage indicates no positions are available
no_positions_indicator = driver.find_elements(By.XPATH, '//div[contains(text(), "No positions found")]')
if no_positions_indicator:
print("No remote and/or hybrid positions are displayed.")
else:
print("Remote and/or hybrid positions are displayed.")
# Close the WebDriver
driver.quit()
2
Answers
It looks like the IDs are dynamically generated with each page load. Instead of using the ID, you can use a CSS selector. There is only one input that is a checkbox.
Two issues:
As pointed out in other answer,
ID
attribute’s value is dynamic. So change that line of code to below:In
headless
mode, sometimes selenium will have trouble clicking on some elements when window is not maximised. So maximise the window as in below code: