user16794957
user16794957

Reputation: 19

SELENIUM FIND ELEMENT BY XPATH

I have three elements with the same button class and span class names. I wanted to use xpath to select the second span class but I always get it wrong.

I used:

browser.find_element(By.XPATH, "/html/body/div[1]/div/div[3]/main/div[3]/div[2]/div[2]/div/div/section[2]/div[2]/form/div[9]/button/span[1]").click()

But I get error. I also tried the following but get the same error message.

result = browser.find_elements(By.XPATH, "//button[@class='_1h5on2Eq5J']")[2].click()

browser.switch_to.frame(result)

results = browser.find_element(By.XPATH, "/html/body/div[1]/div/div[3]/main/div[3]/div[2]/div[2]/div/div/section[2]/div[2]/form/div[9]/button/span[1]")

Below is an excerpt of the html. My interest is to find the second element.

enter image description here

Upvotes: 1

Views: 555

Answers (1)

Michael Mintz
Michael Mintz

Reputation: 15381

You can use xpath to find text that contains a substring.

Here's the selector you would want for the "Expand" button:

"//button//span[contains(., 'Expand')]"

Upvotes: 2

Related Questions