Rayrun
โ† Back to Discord Forum

Does anyone know how to store the value found from the ContainText function

I'm trying to write a program that search a text for a certain string, and then I want to use the found string and store it in a variable to be used repetively in other parts of my program

This thread is trying to answer question "How can one store the value found from the ContainText function in a variable for repetitive use in other parts of a program?"

8 replies

Sure. You can run the toContainText assertion to locate and verify your element, then use const text = myLocatorThatContainsTheDesiredText.textContent() then use the text variable wherever you want.

https://playwright.dev/docs/api/class-locator#locator-text-content

Close but won't work.. const text = myLocatorThatContainsTheDesiredText.textContent() Should be: const text =await myLocatorThatContainsTheDesiredText.textContent()

The await was missed...

Oh sorry. To my defense I was on my phone ๐Ÿ˜…

Thank for fixing the issue

Sorry, my co-workers call me Mr. Lint ๐Ÿ˜‰

Thanks I will try it

await (expect (page.getByText('+===================================+ | New Account Info | +====================')).toContainText('NameServer1: ns1.10-2-68-250.cprapid.com')); await (expect (page.getByText('+===================================+ | New Account Info | +====================')).toContainText('NameServer2: ns2.10-2-68-250.cprapid.com'));

I want to store NameServer2: ns2.10-2-68-250.cprapid.com into a value

I'm literally verifying text that is present in an output page with alot of other text. However, I want to store the specified text into a variable for later use/comparison

Answer

Storing Text Search Results in Variables

You can store the result of a text search in a variable for future use in your program. Here's how to do it in TypeScript with @playwright/test.

First, declare a variable and assign it the text you're searching for:

const searchText = 'orange';

Next, use the getByText() function to find an element with that text content. Store the result in another variable:

const element = await page.getByText(searchText);

Now, you can reuse element as needed in your program. For example, if you want to click on the element multiple times, you can do so like this:

await element.click();
// ... other code ...
await element.click();
// ... more code ...

Remember, variables are scoped within their respective blocks or functions. If you need to access the stored value across different parts of your program, make sure to define your variables at an appropriate scope.

Also, if your program involves asynchronous operations or awaits for promises, handle them properly using async/await syntax or promise chaining.

Storing values from a text search in variables allows you to conveniently access and manipulate those values throughout your program whenever needed.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].