Get InnertText from input with pupperteer-sharp using C# and XPath

312 Views Asked by At

How to get the innerText from a <input> using XPath?

I have this to work but with CSS, but it do not work for me with XPath

    IElementHandle ha = await page.QuerySelectorAsync(xpath);
    IJSHandle ith = await ha.GetPropertyAsync("value");
    string val = ith.RemoteObject.Value.ToString();
1

There are 1 best solutions below

0
Martin Honnen On

The following works for me:

using PuppeteerSharp;

using var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });

using (var page = await browser.NewPageAsync())
{
    await page.GoToAsync("http://www.google.de/");
    var input = (await page.XPathAsync("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[4]/center/input[1]"))[0];
    var ith = await input.GetPropertyAsync("value");
    var val = ith.RemoteObject.Value.ToString();
    Console.Write(val);
}