Home > Net >  How to select button inside of multiple classes with selenium python?
How to select button inside of multiple classes with selenium python?

Time:08-14

I have a page with multiple elements, it's called cards, and on this page I want to click a button on some cards but not on all. The problem is the structure of individual cards because only class of card is different but class inside of cards and button are the same. I'm not sure how to find a right card.

Usually I use something like this, but in this case i want to select certain cards.

browser.find_element(By.CLASS_NAME, 'button-class').click()

But in this case I have something like this...

<div id="card-summer_1">
    <div >
        <div >
            <button >
            </button>
        </div>
    </div>
</div>
<div id="card-summer_2">
    <div >
        <div >
            <button >
            </button>
        </div>
    </div>
</div>
<div id="card-summer_3">
    <div >
        <div >
            <button >
            </button>
        </div>
    </div>
</div>

How to click on button on id id="card-summer_1" and id="card-summer_3"?

Thanks in advance!

CodePudding user response:

Find elements instead by CSS selector:

find_element(By.CSS_SELECTOR, ".card-summer_1 button")

CodePudding user response:

By XPATH:

driver.find_element(By.XPATH, "//div[@id='card-summer_1']//button"]).click()

driver.find_element(By.XPATH, "//div[@id='card-summer_3']//button"]).click()

By CSS_SELECTOR:

driver.find_element(By.By.CSS_SELECTOR, ".card-summer_1 button").click()
driver.find_element(By.By.CSS_SELECTOR, ".card-summer_3 button").click()
  • Related