question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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 issue

Hi,

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:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
mathewr94commented, May 20, 2022

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!

1reaction
gauravgandhi1315commented, May 19, 2022

@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))

Read more comments on GitHub >

github_iconTop Results From Across the Web

Playwright error (Target closed) after navigation - Stack Overflow
eval to retrieve the textContent it works perfectly, there are no target closed errors anymore. try { const context = await browser.
Read more >
playwright._impl._api_types.error: execution context was ...
$eval to retrieve the textContent it works perfectly, there are no target closed errors anymore. try { const context = await ...
Read more >
HTML DOM Element textContent Property - W3Schools
The textContent property sets or returns the text content of the specified node, and all its descendants. Note. When you set the textContent...
Read more >
Docs • Svelte
If you export a const , class or function , it is readonly from outside the component. Functions are valid prop values, however,...
Read more >
Drag operations - Web APIs - MDN Web Docs - Mozilla
The draggable attribute may be used on any element, including images and links. However, for these last two, the default value is true...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found