0

I am trying to click these buttons with Selenium:

<button class="MuiButtonBase-root MuiButton-root jss38 MuiButton-outlined MuiButton-outlinedPrimary" tabindex="0" type="button"><span class="MuiButton-label">Twist up</span><span class="MuiTouchRipple-root"></span></button>

and

<button class="MuiButtonBase-root MuiButton-root jss38 MuiButton-contained MuiButton-containedPrimary" tabindex="0" type="button"><span class="MuiButton-label">Yes</span><span class="MuiTouchRipple-root"></span></button>

I have tried:

driver.find_element(By.XPATH, "//button[@class='MuiButtonBase-root MuiButton-root jss38 MuiButton-outlined MuiButton-outlinedPrimary']//span[@class='MuiButton-label' and contains('., up')]").click()

and

driver.find_element(By.XPATH, "//button[@class='MuiButtonBase-root MuiButton-root jss38 MuiButton-outlined MuiButton-outlinedPrimary']//span[@class='MuiButton-label' and contains('., Yes')]").click()

I can't do it by the class name, because there are other buttons with the same class name and the static xpath values change, so i'm trying to do it this way but must be missing the gist somewhere.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Long John
  • 13
  • 3

2 Answers2

0

The element with text Twist up have the unique class MuiButton-outlinedPrimary so you can use:

driver.find_element(By.XPATH, "//button[contains(@class, 'MuiButton-outlinedPrimary')]//span[@class='MuiButton-label' and contains(., 'Twist up')]").click()

The element with text Yes have the unique class MuiButton-containedPrimary so you can use:

driver.find_element(By.XPATH, "//button[contains(@class, 'MuiButton-containedPrimary')]//span[@class='MuiButton-label' and contains(., 'Yes')]").click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
<button class="MuiButtonBase-root MuiButton-root jss38 MuiButton-outlined MuiButton-outlinedPrimary" tabindex="0" type="button"><span class="MuiButton-label">Twist up</span><span class="MuiTouchRipple-root"></span></button>

For the top one, you can use:

//span[text()='Twist up']/..

This will give you the direct parent of the span with text Twist up

<button class="MuiButtonBase-root MuiButton-root jss38 MuiButton-contained MuiButton-containedPrimary" tabindex="0" type="button"><span class="MuiButton-label">Yes</span><span class="MuiTouchRipple-root"></span></button>

Here you can use the same xpath with text Yes

//span[text()='Yes']/..
Anand
  • 1,899
  • 1
  • 13
  • 23