How do I use Playwright to find a <div> element by its class name and navigate to its href value?


Sure, let's dive right in!

First, you'll need to locate the <div> element with the class name. Let's say the class name is "my-div". You can use Playwright's page.locator() method with a CSS selector to find it:

const divElement = await page.locator('.my-div');

Next, you'll want to get the href value of this element. You can do this using the .getAttribute() method:

const hrefValue = await divElement.getAttribute('href');

Now, you have the href value! You can use it as you wish. For instance, if you want to open this hyperlink in a new tab, you can use the .goto() method:

await page.goto(hrefValue);

Remember, this is just one way to interact with a <div> element based on its class name and href value using Playwright. Your approach may vary depending on your specific needs.

For more insights on how to efficiently use Playwright, check out this blog post.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

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 luc@ray.run.