Esempio n. 1
0
        private static async Task <ElementHandle> GetElement(Frame frame, FindElementBy findBy, string identifier, int index)
        {
            var elements = findBy == FindElementBy.XPath
                ? await frame.XPathAsync(identifier)
                : await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier));

            return(elements[index]);
        }
Esempio n. 2
0
        public static async Task PuppeteerSwitchToFrame(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];
            data.Objects["puppeteerFrame"] = await elem.ContentFrameAsync();

            data.Logger.Log($"Switched to iframe", LogColors.DarkSalmon);
        }
Esempio n. 3
0
        public static void SeleniumClearField(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);

            element.Clear();

            data.Logger.Log("Cleared the field", LogColors.JuneBud);
        }
Esempio n. 4
0
        public static int SeleniumGetHeight(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);
            var height  = element.Size.Height;

            data.Logger.Log($"The height of the element is {height}", LogColors.JuneBud);
            return(height);
        }
Esempio n. 5
0
        public static int SeleniumGetPositionY(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);
            var y       = element.Location.Y;

            data.Logger.Log($"The Y coordinate of the element is {y}", LogColors.JuneBud);
            return(y);
        }
Esempio n. 6
0
        public static int SeleniumGetWidth(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);
            var width   = element.Size.Width;

            data.Logger.Log($"The width of the element is {width}", LogColors.JuneBud);
            return(width);
        }
Esempio n. 7
0
        public static bool SeleniumIsEnabled(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);
            var enabled = element.Enabled;

            data.Logger.Log($"Found out the element is{(enabled ? "" : " not")} enabled", LogColors.JuneBud);
            return(enabled);
        }
Esempio n. 8
0
        public static async Task PuppeteerUploadFiles(BotData data, FindElementBy findBy, string identifier, int index, List <string> filePaths)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem  = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];
            await elem.UploadFileAsync(filePaths.ToArray());

            data.Logger.Log($"Uploaded {filePaths.Count} files to the element", LogColors.DarkSalmon);
        }
Esempio n. 9
0
        public static async Task PuppeteerSelect(BotData data, FindElementBy findBy, string identifier, int index, string value)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem  = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];
            await elem.SelectAsync(value);

            data.Logger.Log($"Selected value {value}", LogColors.DarkSalmon);
        }
Esempio n. 10
0
        public static void SeleniumSelectByText(BotData data, FindElementBy findBy, string identifier, int index, string text)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);

            new SelectElement(element).SelectByText(text);
            UpdateSeleniumData(data);

            data.Logger.Log($"Selected text {text}", LogColors.JuneBud);
        }
Esempio n. 11
0
        public static async Task PuppeteerSelectByText(BotData data, FindElementBy findBy, string identifier, int index, string text)
        {
            data.Logger.LogHeader();

            var frame      = GetFrame(data);
            var elemScript = GetElementScript(findBy, identifier, index);
            var script     = $"let el={elemScript};for(let i=0;i<el.options.length;i++){{if(el.options[i].text=='{text}'){{el.selectedIndex = i;break;}}}}";
            await frame.EvaluateExpressionAsync(script);

            data.Logger.Log($"Selected text {text}", LogColors.DarkSalmon);
        }
Esempio n. 12
0
        public static void SeleniumSubmit(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);

            element.Submit();
            UpdateSeleniumData(data);

            data.Logger.Log($"Submitted the form", LogColors.JuneBud);
        }
Esempio n. 13
0
        public static void SeleniumClick(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);

            element.Click();
            UpdateSeleniumData(data);

            data.Logger.Log("Clicked the element", LogColors.JuneBud);
        }
Esempio n. 14
0
        public static void SeleniumSwitchToFrame(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var browser = GetBrowser(data);
            var element = GetElement(data, findBy, identifier, index);

            browser.SwitchTo().Frame(element);

            data.Logger.Log($"Switched to iframe", LogColors.JuneBud);
        }
Esempio n. 15
0
        public static List <string> SeleniumGetAttributeValueAll(BotData data, FindElementBy findBy, string identifier,
                                                                 string attributeName = "innerText")
        {
            data.Logger.LogHeader();

            var elements = GetElements(data, findBy, identifier);
            var values   = elements.Select(e => e.GetAttribute(attributeName)).ToList();

            data.Logger.Log($"Got {values.Count} values for attribute {attributeName}", LogColors.JuneBud);
            return(values);
        }
Esempio n. 16
0
        public static string SeleniumGetAttributeValue(BotData data, FindElementBy findBy, string identifier, int index,
                                                       string attributeName = "innerText")
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);
            var value   = element.GetAttribute(attributeName);

            data.Logger.Log($"Got value {value} of attribute {attributeName}", LogColors.JuneBud);
            return(value);
        }
Esempio n. 17
0
        public static async Task PuppeteerSelect(BotData data, FindElementBy findBy, string identifier, int index, string value)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem  = await GetElement(frame, findBy, identifier, index);

            await elem.SelectAsync(value);

            data.Logger.Log($"Selected value {value}", LogColors.DarkSalmon);
        }
Esempio n. 18
0
        private static IWebElement GetElement(BotData data, FindElementBy findBy, string identifier, int index)
        {
            var elements = GetElements(data, findBy, identifier).ToArray();

            if (elements.Length < index + 1)
            {
                throw new Exception($"Expected at least {index + 1} elements to be found but {elements.Length} were found");
            }

            return(elements[index]);
        }
Esempio n. 19
0
        public static async Task PuppeteerSwitchToFrame(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem  = await GetElement(frame, findBy, identifier, index);

            data.SetObject("puppeteerFrame", await elem.ContentFrameAsync());

            data.Logger.Log($"Switched to iframe", LogColors.DarkSalmon);
        }
Esempio n. 20
0
        public static async Task PuppeteerSubmit(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var elemScript = GetElementScript(findBy, identifier, index);
            var frame      = GetFrame(data);
            var script     = elemScript + ".submit();";
            await frame.EvaluateExpressionAsync(script);

            data.Logger.Log($"Submitted the form by executing {script}", LogColors.DarkSalmon);
        }
Esempio n. 21
0
        public static void SeleniumSelectByIndex(BotData data, FindElementBy findBy, string identifier, int index, int selectionIndex)
        {
            data.Logger.LogHeader();

            var element = GetElement(data, findBy, identifier, index);

            new SelectElement(element).SelectByIndex(selectionIndex);
            UpdateSeleniumData(data);

            data.Logger.Log($"Selected value at index {selectionIndex}", LogColors.JuneBud);
        }
Esempio n. 22
0
        public static async Task <int> PuppeteerGetPositionY(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem  = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];
            var y     = (int)(await elem.BoundingBoxAsync()).Y;

            data.Logger.Log($"The Y coordinate of the element is {y}", LogColors.DarkSalmon);
            return(y);
        }
Esempio n. 23
0
        public static async Task <int> PuppeteerGetHeight(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var frame  = GetFrame(data);
            var elem   = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];
            var height = (int)(await elem.BoundingBoxAsync()).Height;

            data.Logger.Log($"The height of the element is {height}", LogColors.DarkSalmon);
            return(height);
        }
Esempio n. 24
0
        public static async Task PuppeteerTypeElement(BotData data, FindElementBy findBy, string identifier, int index,
            string text, int timeBetweenKeystrokes = 0)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];
            await elem.TypeAsync(text, new PuppeteerSharp.Input.TypeOptions { Delay = timeBetweenKeystrokes });

            data.Logger.Log($"Typed {text}", LogColors.DarkSalmon);
        }
Esempio n. 25
0
        public static async Task PuppeteerWaitForElement(BotData data, FindElementBy findBy, string identifier, bool hidden = false, bool visible = true,
            int timeout = 30000)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            await frame.WaitForSelectorAsync(BuildSelector(findBy, identifier),
                new PuppeteerSharp.WaitForSelectorOptions { Hidden = hidden, Visible = visible, Timeout = timeout });

            data.Logger.Log($"Waited for element with {findBy} {identifier}", LogColors.DarkSalmon);
        }
Esempio n. 26
0
 private static string GetElementsScript(FindElementBy findBy, string identifier)
 {
     if (findBy == FindElementBy.XPath)
     {
         var script = $"document.evaluate(\"{identifier.Replace("\"", "\\\"")}\", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)";
         return($"Array.from({{ length: {script}.snapshotLength }}, (_, index) => {script}.snapshotItem(index))");
     }
     else
     {
         return($"document.querySelectorAll('{BuildSelector(findBy, identifier)}')");
     }
 }
Esempio n. 27
0
        public static async Task PuppeteerSetAttributeValue(BotData data, FindElementBy findBy, string identifier, int index,
                                                            string attributeName, string value)
        {
            data.Logger.LogHeader();

            var elemScript = GetElementScript(findBy, identifier, index);
            var frame      = GetFrame(data);
            var script     = elemScript + $".setAttribute('{attributeName}', '{value}');";
            await frame.EvaluateExpressionAsync(script);

            data.Logger.Log($"Set value {value} of attribute {attributeName} by executing {script}", LogColors.DarkSalmon);
        }
Esempio n. 28
0
        public static async Task <bool> PuppeteerIsDisplayed(BotData data, FindElementBy findBy, string identifier, int index)
        {
            data.Logger.LogHeader();

            var elemScript = GetElementScript(findBy, identifier, index);
            var frame      = GetFrame(data);
            var script     = $"window.getComputedStyle({elemScript}).display !== 'none';";
            var displayed  = await frame.EvaluateExpressionAsync <bool>(script);

            data.Logger.Log($"Found out the element is{(displayed ? "" : " not")} displayed by executing {script}", LogColors.DarkSalmon);
            return(displayed);
        }
Esempio n. 29
0
        public static async Task PuppeteerClick(BotData data, FindElementBy findBy, string identifier, int index,
            PuppeteerSharp.Input.MouseButton mouseButton = PuppeteerSharp.Input.MouseButton.Left, int clickCount = 1,
            int timeBetweenClicks = 0)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];
            await elem.ClickAsync(new PuppeteerSharp.Input.ClickOptions { Button = mouseButton, ClickCount = clickCount, Delay = timeBetweenClicks });

            data.Logger.Log($"Clicked {clickCount} time(s) with {mouseButton} button", LogColors.DarkSalmon);
        }
Esempio n. 30
0
        public static async Task<string> PuppeteerScreenshotBase64(BotData data, FindElementBy findBy, string identifier, int index,
            bool fullPage = false, bool omitBackground = false)
        {
            data.Logger.LogHeader();

            var frame = GetFrame(data);
            var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];
            var base64 = await elem.ScreenshotBase64Async(new PuppeteerSharp.ScreenshotOptions { FullPage = fullPage, OmitBackground = omitBackground });

            data.Logger.Log($"Took a screenshot of the element as base64", LogColors.DarkSalmon);
            return base64;
        }