'Python Selenium Pressing Button

I am using Selenium to try and press this button, but Selenium complains that it does not exists

<button name="button" type="submit" class="btn btn-primary"><span class="show-when-enabled"><svg class="bi flex-shrink-0 me-2" height="24" role="img" width="24"><use href="#icon_content-save"></use></svg><span class="pe-3">Save Database/Server</span></span><span class="show-when-disabled"><span class="spinner-border spinner-border-sm"></span><span class="px-3">Please wait, saving record...</span></span></button>

Here is the first command

driver.find_element_by_css_selector("button[type=submit]").click()

This is the second

driver.find_element(By.LINK_TEXT, "Save Database/Server").click() 

and the third command

driver.find_element(By.XPATH, "//a[.//span[text()='Save Database/Server']]").click() 

All three error because Selenium says it does not exists. How should I press the submit button?



Solution 1:[1]

Your xpath and css selector seems wrong. there is no anchor tag.

It should have been. XPATH:

driver.find_element(By.XPATH, "//button[.//span[text()='Save Database/Server']]").click() 

Css Selector:

driver.find_element(By.CSS_SELECTOR,"button[type='submit']").click()

LINK_TEXT supports only for anchor tag not button tag.

Solution 2:[2]

Have you tried using wait and action if not use the below.

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains


time.sleep(5)
driver.find_element_by_xpath("yourxPath").click()


WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "YourXpath"))).click()


time.sleep(5)
button = driver.find_element_by_xpath("YourXpath")
driver.execute_script("arguments[0].click();", button)


time.sleep(5)
button = driver.find_element_by_xpath("Xpath")
ActionChains(driver).move_to_element(button).click().perform()

Solution 3:[3]

Try it may be work

driver.execute_script("arguments[0].click();", button)

And can you write down the mistake you made?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KunduK
Solution 2 Akzy
Solution 3 Ha Luu