Search code examples
pythonpython-3.xselenium-webdriverbrowser-automationselenium-edgedriver

How do I click this button using Selenium in Python? (Using Edge browser)


I have been trying to automate the login into a booking system to help me to book for my classes.

enter image description here

I have tried to use the following methods:

clickLogin = driver.find_element(By.XPATH, "//button[text()='Access to Booking System']")
driver.execute_script("arguments[0].click();", clickLogin)

clickLogin = driver.find_element(By.XPATH, "//button[text()='Access to Booking System']")
clickLogin.click()

clickLogin = driver.find_element(By.CLASS_NAME, "div.v-btn.v-btn--is-elevated.v-btn--has-bg.theme--light.v-size--default.primary")
clickLogin.click()

But still doesnt seem to be clicking it.

The rest of the code is here, if anyone needs context: https://github.com/jaezeu/drive-booking/blob/main/main.py


Solution

  • Your HTML is behind a login screen so I can't see the full HTML but here's what I can tell from your posted HTML...

    The issue with the XPath in the first two locators is that the text inside the element is flanked by spaces. It looks like it should be

    driver.find_element(By.XPATH, "//button[text()=' Access to Booking System ']").click()
                                                    ^      spaces added      ^
    

    The issue with the third locator is that you've specified By.CLASS_NAME but provided a CSS selector. By.CLASS_NAME only takes a single class name so you'd need to convert it to a CSS selector anyway. The other issue is that your CSS selector is looking for a DIV but it's actually a BUTTON in the HTML you provided. The below is the fixed CSS selector.

    driver.find_element(By.CSS_SELECTOR, "button.v-btn.v-btn--is-elevated.v-btn--has-bg.theme--light.v-size--default.primary").click()
    

    Either of these should work. I would prefer the XPath because to me it's more human readable with the contained text so if anything changes on the page and the locator breaks, it's easier to see what changed vs a string of a bunch of obscure class names.