Home > OS >  Get CSS_SELECTOR or XPATH of a Button Selenium Python
Get CSS_SELECTOR or XPATH of a Button Selenium Python

Time:10-02

Hello I'm trying to click on a button but it's not working for me what I have tried so far is as follows:

enter image description here

driver.find_elements_by_id("id__1115").click()
driver.find_elements_by_css_selector('.ms-Button.ms-Button--default.manageOwnersButtonStyle-1007').click()
driver.find_elements_by_xpath("/html[1]/body[1]/div[5]/div[1]/div[1]/div[1]/div[1]/div[3]/div[2]/div[3]/div[2]/div[1]/div[1]/div[1]/div[3]/button[1]/span[1]/span[1]/span[1]").click()
driver.find_elements_by_xpath("/html[1]/body[1]/div[5]/div[1]/div[1]/div[1]/div[1]/div[3]/div[2]/div[3]/div[2]/div[1]/div[1]/div[1]/div[3]/button[1]").click()

CodePudding user response:

elements and click throwing error instead element and click because elements produce list and list is not clickable in selenium.

I changed my code to :

driver.find_element_

Only this xpath worked:

driver.find_element_by_xpath("/html[1]/body[1]/div[5]/div[1]/div[1]/div[1]/div[1]/div[3]/div[2]/div[3]/div[2]/div[1]/div[1]/div[1]/div[3]/button[1]/span[1]/span[1]/span[1]").click()

thanks

I don't how many elements. For example: (using xpath is better)

el = driver.find_elements_by_id("id__1115")
el[2].click()

CodePudding user response:

If Add owners text is not gonna change then

You can use the below xpath

//span[text()='Add owners']/ancestor::button

Click on it like

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//span[text()='Add owners']/ancestor::button").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add owners']/ancestor::button"))).click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

  • Related