Selenium find element by class breaking code


My program checks a login page for available combinations. Upon first opening up the page, there is a "check availability" button, which has an ID which Selenium is able to find/use to click.

If the username is taken, a new button pops up which says "check another combination", however the DOM inspector only gives the following information about the second button:

<a class="btn btn-outline-primary btn-block mt-4" href="" role="button" data-ng-click="combination.reset()">
                                    Check another combination

I have tried finding this button by CLASS, CLASSNAME, copying and pasting the XPATH from the inspector and all these have lead to not only the button clicking not working, but the entering and submitting of the intial combination also ceasing to work.

Hoping someone can suggest how to identify this button from the given information. For reference, here is one of the lines I tried:

check_another_combo_button = driver.find_element(By.CLASS, "btn btn-outline-primary btn-block mt-4")

Here is my full code:

from selenium import webdriver
from import By
from webdriver_manager.firefox import GeckoDriverManager
import time

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
site = "https://www......"

combination_input_field = driver.find_element(By.ID, "userCombination")
check_availability_button = driver.find_element(By.ID, "btn_fQPAC_submit")

combination_input_field.send_keys(77777) #77777 is one of the unavailable combos
check_another_combo_button = driver.find_element(By.CLASS_NAME, "btn btn-outline-primary btn-block mt-4")


I suspect you want to check for inputs say inputs in a loop. Here is the solution using both chrome and firefox.

Solution using chrome driver :

from selenium import webdriver
from import Service
from import By
from import expected_conditions as EC
from import WebDriverWait

chrome_path = r"C:\Users\hpoddar\Desktop\Tools\chromedriver_win32\chromedriver.exe"

s = Service(chrome_path)
url = 'https://www......'
driver = webdriver.Chrome(service=s)

inputs = ['77777', '88888', '99999', '11111']
for plate in inputs:
    combination_input_field = driver.find_element(By.ID, "qPlateCombination")
    check_availability_button = driver.find_element(By.ID, "btn_fQPAC_submit")

    element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "")))
    check_another_combo_button = driver.find_element(By.CSS_SELECTOR, "")

Solution using Geckodriver :

To use Geckodriver, all you need to add is

from webdriver_manager.firefox import GeckoDriverManager
from import Service

and change driver initialization line to

s = Service(executable_path=GeckoDriverManager().install())

Answered By – Himanshu Poddar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published