4

I have been trying to learn playwright and during the course of implementation I am trying to write a test case which find an input element by Name and fills it with some values

Unfortunately, the element has only class and name attributes. I have been trying to find a way to read it but did not found anything on the docs or I might have missed

Kindly help

Vivek
  • 156
  • 3
  • 18

1 Answers1

4

Not direct answer, but a work around. You can specify your css selector to search for the name attribute:

Structure is:

<tag>[attribute='value']

For example: This html (which is on this page if you view the dom):

<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0">

Is identified with this css:

meta[name='viewport']

It is also possible without a tag and only by the name, see the following example:

await page.click(`[name='my-value']`);

screenshot of devtools with identifier

Max Schmitt
  • 2,529
  • 1
  • 12
  • 26
RichEdwards
  • 3,423
  • 2
  • 6
  • 22
  • one more question - I found multiple element with the same name and playwright used the first one, which happened to be a hidden field, which is causing the same to fail. is there anything to tell playwright to selected a specific one ? I tried giving the following `await page.fill("[name='username'] :visible", "admin");` but obviously its not working, any further suggestions? – Vivek Aug 07 '21 at 16:22
  • @vivek try a different locator? A more specific one? - just using `[name='username']` will find any element of any tag with that name and, as you've seen, might not pick the one you want. Easiest solution is to write a better, more specific identifier. If you share your html (probably as a new question) someone will help - alternatively, I strongly suggest you invest a couple of hours and learn how to identify web elements. This is an excellent learning resource and will teach you do to it like a pro: https://testautomationu.applitools.com/web-element-locator-strategies/ – RichEdwards Aug 09 '21 at 08:09
  • Also - `page.$$(selector)` is how you can collect all elements and iterate through. https://playwright.dev/docs/api/class-page#page-query-selector-all – RichEdwards Aug 09 '21 at 08:12
  • ya that has been a challenge, unfortunately we are dependent on a 3rd party application which seems to have caused these set backs ! Thanks for the help. made quite a bit of progress. – Vivek Aug 10 '21 at 11:57