-1

I skimmed through similar questions, but all questions seem to be focused around 'locating span tag by text', but my goal is to 'get all texts within span tag'.

ex)

from playwright.sync_api import sync_playwright

    browser = pw.chromium.launch(headless=False)
    context = browser.new_context(viewport={"width": 1920, "height": 1080})
    page = context.new_page()

    url = 'https://announcements.bybit.com/en-US/?category=new_crypto&page=6'

    page.goto(url)
    page.wait_for_timeout(2000)

    print(page.get_by_test_id('//span').inner_text())

    x = page.locator("//span[text()='New Listing: TAMA/USDT — Grab a Share of the 4,000,000 TAMA Prize Pool']")
    x.click()

the above code detects a span tag by text, and clicks on it.

However, I cannot find a way to locate all tags, and retrive all texts within the span tags.

Is there any possible way to do so?

petezurich
  • 9,280
  • 9
  • 43
  • 57
Peter Yoo
  • 9
  • 1

2 Answers2

0
# Find the <span> element using a CSS selector
span_element = page.query_selector("span")

# Get the text content within the <span> element
text = span_element.text_content()
print("Text within <span>:", text)
petezurich
  • 9,280
  • 9
  • 43
  • 57
0

Use all() to get a list of all span elements:

all_spans = page.locator('//span').all()

for span in all_spans:
    print(span.text_content())
candre
  • 593
  • 3
  • 8