Home > Net >  Python Selenium Pressing Button
Python Selenium Pressing Button

Time:03-18

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

<button name="button" type="submit" ><span ><svg  height="24" role="img" width="24"><use href="#icon_content-save"></use></svg><span >Save Database/Server</span></span><span ><span ></span><span >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?

CodePudding user response:

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.

CodePudding user response:

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()

CodePudding user response:

Try it may be work

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

And can you write down the mistake you made?

  • Related