简体   繁体   English

如何使用 Pupeetersharp 从元素中获取计算的样式属性

[英]How can I get computed style property from element with Pupeetersharp

I am trying to use PuppeteerSharp to get the computed color property of an element.我正在尝试使用 PuppeteerSharp 来获取元素的计算颜色属性。 I have seen several answers using Pupeeteer (not Sharp) indicating that window.getComputedStyle() might be the solution but I can't get it to work using PuppeteerSharp.我已经看到使用 Puppeteer(不是 Sharp)的几个答案表明 window.getComputedStyle() 可能是解决方案,但我无法使用 PuppeteerSharp 使其工作。

See :看 :

So far my code looks like this :到目前为止,我的代码如下所示:

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
using (browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = IsHeadless }))
{
    using (var page = await Program.browser.NewPageAsync())
    {
        var jsCode = @"async() => {
        const element = document.querySelector('.productDetail svg[viewbox=""0 0 16 16""]');
        return window.getComputedStyle(element).getPropertyValue('color')};
        ;}";

        await page.GoToAsync("https://www.galaxus.ch/fr/s3/product/horizon-fitness-syros-e-velos-elliptiques-13874309");
        var results = await page.EvaluateFunctionAsync<string>(jsCode);
     }
}

Any help would be appreciated !任何帮助,将不胜感激 !

I found a few issues in your code.我在您的代码中发现了一些问题。 First a few small errors in your javascript code: you don't need to async and an extra brackets.首先是您的 javascript 代码中的一些小错误:您不需要异步和额外的括号。

var jsCode = @"() => {
  const element = document.querySelector('.productDetail svg[viewbox=""0 0 16 16""]');
  return window.getComputedStyle(element).getPropertyValue('color');
}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM