-1

How do I get the inner text of an element that has been click?

string xplast = "//button[@id='test']";
IElementHandle last = await ((IPage)page).WaitForXPathAsync(xplast);
await last.FocusAsync();
await last.ClickAsync();
string innertext = (await last.GetInnerTextFromElement()).ToString()

is there a way to do something like this? string innertext = (await last.GetInnerTextFromElement()).ToString()

  • Doesn't the code for the previous question/answer work here as well e.g. `var text = (await last.GetPropertyAsync("innerText")).RemoteObject.Value.ToString());`? – Martin Honnen Jan 18 '23 at 17:34
  • Hi Martin, sorry yes, I realized later that it's kind of same. Sometime this is just too confusing, and even it's the same answer, I just did not realize that, Sorry for this – PeterSchwennesen Jan 24 '23 at 08:32

2 Answers2

1

I have now tested that var text = (await last.GetPropertyAsync("innerText")).RemoteObject.Value.ToString(); works fine, at least here on Windows.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

If you'd prefer a strongly typed experience then PuppeteerSharp.Dom provides a set of extensions to PuppeteerSharp.

Install PuppeteerSharp.Dom from Nuget.org then you can use the strongly typed extensions.

// Add using PuppeteerSharp.Dom; to access WaitForXPathAsync<T>

string xplast = "//button[@id='test']";
var last = await page.WaitForXPathAsync<HtmlButtonElement>(xplast);
await last.FocusAsync();
await last.ClickAsync();
var innerText = await last.GetInnerTextAsync();
amaitland
  • 4,073
  • 3
  • 25
  • 63