[Question] Why do I keep getting a "Target closed" error when I attempt to get the text content from an element via locator.textContent()?
See original GitHub issueHi,
Please bear in mind, I’m brand new to Playwright (and TypeScript), so this is likely a very stupid mistake on my behalf. That said, here’s the offending code snippet:
const currentDressPriceString: string = await this.page.locator(DressesPage.dressesPrices).nth(i).textContent();
When my test gets to that line of code, it fails and gives me a “locator.textContent: Target closed” error in the console. I’ve Googled this extensively and it doesn’t seem like many others have came across this exact thing.
In case this helps at all, the DressesPage.dressesPrices locator I’m using is:
static readonly dressesPrices = "div.right-block > div.content_price > span.price";
and the webpage that I’m using it on is http://automationpractice.com/index.php?id_category=8&controller=category
Thanks, Mathew
Issue Analytics
- State:
- Created a year ago
- Comments:9 (3 by maintainers)
Morning, afternoon or evening (depending on where you’re from). I’ve figured it out and it truly was a rookie mistake… the nth() function indexing starts at 0, not 1. Not sure why I assumed it would be 1. Everyone feel free to boo me when you read this. Thanks anyway!
@oytunerdd code looks good to me. To print all the data in the list, you can probably use:
let selector = "div.right-block > div.content_price > span.price";
const getData = await this.page.$$eval(selector, (options) => options.map((option) => option.textContent))