4

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!')
1
  • Side note: it does work sometimes, sometimes not. Also, I don't get any errors, it just tells me that it's been clicked even if it doesn't go through. Commented Aug 11, 2022 at 19:13

1 Answer 1

5

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
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.