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]); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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]); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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)}')"); } }
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); }
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); }
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); }
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; }