private string GetSelector(SelectorAttribute attribute, Page page) { if (!string.IsNullOrEmpty(attribute.Path)) { var element = page.Selector.Selector(attribute.Path, attribute.PathType); if (element == null) { return(null); } if (attribute.SelectorMatch == SelectorMatch.InnerHtml) { return(element.InnerHtml); } else if (attribute.SelectorMatch == SelectorMatch.InnerText) { return(element.InnerText); } else if (attribute.SelectorMatch == SelectorMatch.OuterHtml) { return(element.OuterHtml); } if (!string.IsNullOrEmpty(attribute.FromAttribute)) { return(element.GetAttribute(attribute.FromAttribute)); } } return(null); }
/// <summary> /// Maps SelectorType to By. /// </summary> /// <param name="selector">Instance of SelectorAttribute.</param> /// <returns>By selector.</returns> public static By MapToSeleniumBy(this SelectorAttribute selector) { switch (selector.Type) { case SelectorType.Id: return(By.Id(selector.Value)); case SelectorType.Name: return(By.Name(selector.Value)); case SelectorType.TagName: return(By.TagName(selector.Value)); case SelectorType.ClassName: return(By.ClassName(selector.Value)); case SelectorType.CssSelector: return(By.CssSelector(selector.Value)); case SelectorType.LinkText: return(By.LinkText(selector.Value)); case SelectorType.PartialLinkText: return(By.PartialLinkText(selector.Value)); case SelectorType.XPath: return(By.XPath(selector.Value)); default: throw new NotSupportedException($"Mapping for selector type: ${selector.Type} is not supported."); } }
private string GetSelectorValue(Page page, SelectorAttribute selectorAttribute) { string result = string.Empty; var selector = selectorAttribute.ToSelector(); if (selectorAttribute.Type == SelectorType.Enviroment) { if (selector is EnviromentSelector enviromentSelector) { result = SelectorUtil.GetEnviromentValue(enviromentSelector.Field, page, 0)?.ToString(); } } else { result = page.Selectable.Select(selector).GetValue(); } if (!string.IsNullOrEmpty(result) && TotalPageFormatters != null) { foreach (var formatter in TotalPageFormatters) { result = formatter.Formate(result)?.ToString(); } } if (string.IsNullOrWhiteSpace(result)) { throw new SpiderException("The result of total selector is null"); } else { return(result); } }
public virtual void FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e) { var crossItem = (INItemXRef)e.Row; if (crossItem?.AlternateType == null || crossItem.AlternateType.IsIn(INAlternateType.CPN, INAlternateType.VPN)) { SelectorAttribute.FieldSelecting(sender, e); } }
public virtual void FieldUpdating(PXCache sender, PXFieldUpdatingEventArgs e) { // suppress PXDimensionSelectorAttribute.FieldUpdating to prevent wrong conversion // from not set value BAccountID = 0: surrogate key may be erroneously interpreted as natural key var crossItem = (INItemXRef)e.Row; if (crossItem?.AlternateType == null || crossItem.AlternateType.IsIn(INAlternateType.CPN, INAlternateType.VPN)) { SelectorAttribute.FieldUpdating(sender, e); } }
public void SelectorAttributeEmptyValuesTest() { SelectorAttribute attr = new SelectorAttribute(".class"); attr.EmptyValues = new string[] { "content" }; HtmlDocument doc = new HtmlDocument(); doc.LoadHtml("<div class='class'>content</div>"); attr.GetValue(doc.DocumentNode, out bool canParse); Assert.AreEqual(false, canParse); }
public void SelectorAttributeEmptyTest() { SelectorAttribute attr = new SelectorAttribute(".not-existing-class"); attr.SkipIfNotFound = true; HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(""); attr.GetValue(doc.DocumentNode, out bool canParse); Assert.AreEqual(false, canParse); }
/// <summary> /// 查找元素 /// </summary> /// <param name="webDriver">WebDriver</param> /// <param name="element">页面元素选择器</param> /// <returns>页面元素</returns> protected IWebElement FindElement(RemoteWebDriver webDriver, SelectorAttribute element) { switch (element.Type) { case SelectorType.XPath: { return(webDriver.FindElementByXPath(element.Expression)); } case SelectorType.Css: { return(webDriver.FindElementByCssSelector(element.Expression)); } } throw new SpiderException("Unsport findy: " + element.Type); }
public static async Task <object> GetReturnValue(this IInvocation invocation, ElementObject elementObject, SelectorAttribute attribute) { var element = elementObject.Element; if (element == null) { return(null); } if (invocation.IsReturning <ElementHandle>()) { return(await element.QuerySelectorAsync(attribute.Selector).ConfigureAwait(false)); } if (invocation.IsReturning <ElementHandle[]>()) { return(await element.QuerySelectorAllAsync(attribute.Selector).ConfigureAwait(false)); } if (invocation.IsReturningElementObject()) { var proxyType = invocation.Method.ReturnType.GetGenericArguments()[0]; var elementHandle = await element.QuerySelectorAsync(attribute.Selector).ConfigureAwait(false); return(ProxyFactory.ElementObject(proxyType, elementObject.Page, elementHandle)); } if (invocation.IsReturningElementObjectArray()) { var arrayType = invocation.Method.ReturnType.GetGenericArguments()[0]; var proxyType = arrayType.GetElementType(); var elementHandles = await element.QuerySelectorAllAsync(attribute.Selector).ConfigureAwait(false); return(ProxyFactory.ElementObjectArray(proxyType, elementObject.Page, elementHandles)); } return(null); }