// GetPropertyValue

        private static async Task <string> GetPropertyValueAsync(this ElementHandle handle, string propertyName)
        {
            handle.GuardFromNull();
            var property = await handle.GetPropertyAsync(propertyName).ConfigureAwait(false);

            return(await property.JsonValueAsync <string>().ConfigureAwait(false));
        }
        private static async Task <string> GetElementTextValueAsync(ElementHandle handle)
        {
            if (handle == null)
            {
                return(string.Empty);
            }

            var innerTextPro = await handle.GetPropertyAsync("innerText");

            var innerTextValue = await innerTextPro.JsonValueAsync <string>();

            return(innerTextValue?.Trim() ?? string.Empty);
        }
示例#3
0
        getUserProfile(LoginInfo loginInfo)
        {
            var browser = await PuppeteerInstance.getInstance();

            using (var page = await browser.NewPageAsync())
            {
                await TwitterPoster.twitterLoginAsync(loginInfo, page);

                var profileButtonHandle = await page.WaitForSelectorAsync(ProfileSelector);

                await profileButtonHandle.ClickAsync();

                await Task.Delay(3000);

                ElementHandle profilePictureHandle     = null;
                ElementHandle profileTitleHandle       = null;
                ElementHandle profileDescriptionHandle = null;
                ElementHandle profileSiteHandle        = null;
                string        imgSrc      = "";
                string        title       = "";
                string        description = "";
                string        site        = "";
                try
                {
                    profilePictureHandle = await page.WaitForSelectorAsync(PictureSelector);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex);
                    profilePictureHandle = null;
                }
                if (profilePictureHandle != null)
                {
                    imgSrc = profilePictureHandle.GetPropertyAsync("src").Result.ToString().Replace("JSHandle:", "");
                }
                await Task.Delay(3000);

                try
                {
                    profileTitleHandle = await page.WaitForSelectorAsync(TitleSelector, new WaitForSelectorOptions { Timeout = 3000 });
                }
                catch (Exception)
                {
                    profileTitleHandle = null;
                }
                try
                {
                    profileDescriptionHandle = await page.WaitForSelectorAsync(DescriptionSelector, new WaitForSelectorOptions { Timeout = 3000 });
                }
                catch (Exception)
                {
                    profileDescriptionHandle = null;
                }
                try
                {
                    profileSiteHandle = await page.WaitForSelectorAsync(SiteSelector, new WaitForSelectorOptions { Timeout = 3000 });
                }
                catch (Exception)
                {
                    profileSiteHandle = null;
                }

                if (profileTitleHandle != null)
                {
                    title = profileTitleHandle.GetPropertyAsync("text").Result.ToString().Replace("JSHandle:", "");
                }
                if (profileDescriptionHandle != null)
                {
                    description = profileDescriptionHandle.GetPropertyAsync("textContent").Result.ToString().Replace("JSHandle:", "");
                }
                if (profileSiteHandle != null)
                {
                    site = profileSiteHandle.GetPropertyAsync("text").Result.ToString().Replace("JSHandle:", "");
                }

                var profilePicture = SaveImage(imgSrc);

                var profile = new Profile
                {
                    picture     = profilePicture,
                    title       = title,
                    description = description,
                    site        = site
                };
                FormControl(profile);
                await page.GoBackAsync();

                await Task.Delay(3000);

                await browser.CloseAsync();

                browser.Dispose();
                return(profile);
            }
        }
        public static async Task <string> SrcAsync(this ElementHandle element)
        {
            var _prop = await element.GetPropertyAsync("src");

            return(await _prop.JsonValueAsync <string>());
        }
        public static async Task <string> TextContentAsync(this ElementHandle element)
        {
            var _prop = await element.GetPropertyAsync("innerText");

            return(await _prop.JsonValueAsync <string>());
        }