pythonselenium-webdriverselenium-chromedriver

Finding Button with Selenium in Python without Name or ID


I am new to Selenium and I am facing a problem, where I have to click on a button which has no ID or Name.

I already tried to find the element by XPath and CSS Selector. The outcome is with both methods the same: The Driver can not find such Element.

HTML: HTML

CSS Selector:

driver.find_element(By.CSS_SELECTOR,"button_text").click()

XPath:

driver.find_element(By.XPATH,"//form\[@data-g-name='Button'\]")

Solution

  • Check the code below with explanation:

    import time
    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
    
    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.get("https://www.bossard.com/eshop/ch-en/cart/")
    wait = WebDriverWait(driver, 10)
    
    # Click on accept all cookies pop-up, use this code if you see the cookie pop-up
    wait.until(EC.element_to_be_clickable((By.ID, "_evidon-banner-acceptbutton"))).click()
    
    # click on Upload from Excel
    wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Upload from Excel']"))).click()
    
    # upload excel
    wait.until(EC.presence_of_element_located((By.XPATH, "//form[@id='uploadXls']//input[@type='file']"))).send_keys("C:/full/path/of/the/file/test.xlsx")
    
    # Click on Add to cart button
    wait.until(EC.element_to_be_clickable((By.XPATH, "//form[@id='uploadXls']//button"))).click()
    time.sleep(20)