microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.
https://playwright.dev/dotnet/
MIT License
2.47k stars 235 forks source link

How to get DOM element by JavaScript GetElementsByClassName in Playwright-Sharp? #1300

Closed zydjohnHotmail closed 3 years ago

zydjohnHotmail commented 3 years ago

Hello: I am rather new to Playwright-Sharp, I need to run some very simple JavaScript in Playwright-Sharp. document.getElementsByClassName('class1')[0].click(); sleep 1 second; document.getElementsByClassName('class2')[0].value = 1; then move focus from document.getElementsByClassName('class2')[0] to other DOM elements. For sleeping 1 second, I can use some JavaScript Promise, but how to move focus after setting an input element value, I don’t know yet. Please show me how I can do this by Playwright-Sharp (version 0.192.0) I searched around, I only see something like for select DOM element: page.WaitForSelectorAsync('#id1'); But this code doesn’t work in my case, as there is no ID# in the whole web page. I have to use getElementsByClassName. By the way, there is no example code on how to set value for an input element. Please advise,

pavelfeldman commented 3 years ago

Class names can be used in the CSS selector: https://playwright.dev/dotnet/docs/selectors Input can be set as follows:

await page.EvalOnSelectorAsync<string>("class-name", "(i, value) => i.value = value", "VALUE");