[Python] how to click a 'single' button from the loop of buttons

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    I am using this snippet of code.

    # separate class locator it lists down all elements
    class AddToCartItem:
    Inventory = "//div[@class='inventory_item']":
    Product_Item_Button = "//button[text()='Add to cart']" #This locator contains x numbers of buttons


    # class and methods to perform the job
    class AddToCart(BasePage):
    def select_item_to_add(self):
    # iterate the buttons
    for selectItem in self.get_elements(AddToCartItem.Product_Item_Button):
    """
    The selectItem.click() clicks all
    the button. how to handle the iteration
    of the elements in this section to click
    a random button from the x number of
    buttons instead of clicking them all.
    """
    selectItem.click()


    Expected: Only want to click/select one button from the iteration of the x numbers of buttons instead of clicking them all.

    Actual: It clicks all the buttons.

    I don't know how to handle it. Very much appreciated your help.

    Continue reading...

Compartilhe esta Página