pythonplaywrightplaywright-python

Playwright Trying to hover multiple items by get_by_text() method


I am trying to find every message that contains "Hello team , here is a new report task:" text in a website and hover them one by one to be able to click another button.

page.get_by_role("treeitem", name="reports-development").locator("div").first.click()

# page.get_by_text(re.compile("Hello team , here is a new report task:")).hover()

text_pattern = re.compile(r"Hello team , here is a new report task:")
elements = page.locator(f'text="{text_pattern.pattern}"')
print(elements.all_inner_texts())
# count = elements.count()
# for i in range(count):
#     elements.nth(i).hover()
#     page.get_by_label("Add reaction…").click()
#     page.get_by_role("gridcell", name="+1 emoji").click()
#     page.wait_for_timeout(5000)  # Wait for 5 seconds

I have tried to iterate it as a locator element first but it didn't work, then tried to use the nth() method but it didn't work too.

How can I iterate the messages I find? I'll have multiple messages on the screen so I have to do the action one by one and to every message I found.


Solution

  • Hope this will be helpful for you.

    from playwright.sync_api import Page
        
    # Assuming 'page' is your Playwright Page object
    
    # Find all elements containing the specified text
    text_pattern = re.compile(r"Hello team , here is a new report task:")
    elements = page.locator(f'text="{text_pattern.pattern}"')
    
    # Iterate over each element and hover over it
    for element in elements:
        element.hover()