Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click on button using Selenium

I'm trying to click a button on a website with Selenium in python, and the button clicking works fine, I can see the button being clicked but the code stops running at that point and when I check the website in my normal browser I can tell that the button hasn't been clicked. More specifically, I'm trying to click a start button for an aternos.org server, and it clicks fine, but the actual starting of the server doesn't go through for some reason.

My code for the start button clicking:

start = driver.find_element(By.ID, 'start')
status = driver.find_element(By.CLASS_NAME, 'statuslabel-label').text
print(status)
if status == 'Offline':
    start.click()
    print('Starting server!')
like image 642
tonster55 Avatar asked Nov 01 '25 13:11

tonster55


1 Answers

Ideally, to locate and click any clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using ID:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "start"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#start"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='start']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
like image 149
undetected Selenium Avatar answered Nov 03 '25 05:11

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!