Search code examples
pythonselenium-webdriver

Press button when class name changes using Selenium


Using Selenium for Edge what would the proper syntax be to click the div below? the class names changes but the "Run Job" text remains the same. How can I click on this using the text instead of the class?

<div class="x9f619 xjbqb8w x78zum5 x168nmei x13lgxp2 x5pf9jr xo71vjh x1n2onr6 x1plvlek xryxfnj x1iyjqo2 x2lwn1j xeuugli xdt5ytf xqjyukv x1qjc9v5 x1oa3qoh x1nhvcw1">Run Job</div>

Solution

  • You can use this xpath

    button= driver.find_element_by_xpath("//div[text()='Run Job']")
    

    Now click on this.

    button.click()
    

    OR using the explicit wait

    driver = webdriver.Edge()
    driver.get("your_website_url_here")
    wait = WebDriverWait(driver, 10)
    
    button= wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Run Job']")))
    
    button.click()
    driver.quit()
    

    import

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