PeterSchwennesen
PeterSchwennesen

Reputation: 31

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

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();

Upvotes: 0

Views: 513

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167471

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);
}

Upvotes: 1

Related Questions