/// <summary> /// 通过属性"xpath"查找元素 /// </summary> /// <param name="elementType"></param> /// <param name="value"></param> /// <returns></returns> public ElementWrapper byXPath(string xpath) { return(Method <ElementWrapper> .Watch("byXPath," + xpath, () => { Constraint c = FindByAttr("XPath", xpath); return find(BrowserExtensions.GetLastTag(xpath), c); })); }
/// <summary> /// 构建鼠标点击事件,同时遍历iframe /// </summary> private void InspectMouseEvent() { mshtml.HTMLDocument htmlDoc = browser.WebbrowserObject.Document as mshtml.HTMLDocument; mshtml.DispHTMLDocument disp = htmlDoc as mshtml.DispHTMLDocument; DHTMLEventHandler onmousedownhandler = new DHTMLEventHandler(htmlDoc); onmousedownhandler.Handler += new DHTMLEvent(Mouse_Down); disp.onmousedown = onmousedownhandler; IHTMLElementCollection col = BrowserExtensions.GetFrames((IHTMLDocument2)htmlDoc); InspectFrameMouseEvent(col); }
protected async Task CheckAccessToken() { try { AccessToken = await BrowserExtensions.ReadLocalStorageAsync(Defaults.AuthTokenStorageName); } catch { // ignored } if (string.IsNullOrEmpty(AccessToken)) { UriHelper.NavigateTo("/authorize"); } }
/// <summary> /// 将选择文本粘帖到主浏览器激活控件上 /// </summary> /// <param name="text"></param> private void DefaultRule(string text) { IHTMLElement active = hostBrowser.GetActiveElement() as mshtml.IHTMLElement; if (active is mshtml.HTMLInputElement) { mshtml.HTMLInputElement input = active as mshtml.HTMLInputElement; input.value = text; } else if (active is mshtml.HTMLTextAreaElement) { mshtml.HTMLTextAreaElement area = active as mshtml.HTMLTextAreaElement; area.innerText = text; } else if (active is mshtml.HTMLBody) { string contentEditable = active.getAttribute("contentEditable", 0) as string; if (!string.IsNullOrEmpty(contentEditable) && contentEditable == "true") { mshtml.HTMLBody body = active as mshtml.HTMLBody; body.innerText = text; return; } IHTMLElement frame = BrowserExtensions.GetFrame(active) as mshtml.IHTMLElement; if (frame != null) { mshtml.HTMLBody body = active as mshtml.HTMLBody; body.innerText = text; } } else { IHTMLElementCollection ec = hostBrowser.GetElementsByTagName(true, "TEXTAREA") as IHTMLElementCollection; if (ec.length > 0) { mshtml.HTMLTextAreaElement area = ec.item(null, 0) as mshtml.HTMLTextAreaElement; area.innerText = text; } else { ec = hostBrowser.GetElementsByTagName(true, "INPUT") as IHTMLElementCollection; mshtml.HTMLInputElement input = ec.item(null, 0) as mshtml.HTMLInputElement; input.value = text; } } }
/// <summary> /// 加载单个树节点 /// </summary> /// <param name="el"></param> /// <param name="dataDict"></param> private void PopulateNode(IHTMLElement el, List <IHTMLElement> dataDict) { if (el == null) { return; } DomTreeNode node = null; if (nodeHash.Contains(el)) { node = nodeHash[el] as DomTreeNode; dataDict.Reverse(); foreach (var o in dataDict) { if (node == null) { continue; } //logger.Info("add treenode:" + XPathFinder.GetXPath(o, true)); IHTMLDOMNode parent = ((IHTMLDOMNode)o).parentNode; if (parent == null) { parent = (IHTMLDOMNode)BrowserExtensions.GetFrame((IHTMLElement)o); } ParseNodes(parent, node, 1); node.IsLoadData = true; node = nodeHash[o] as DomTreeNode; } } else { dataDict.Add(el); IHTMLDOMNode cnode = el as IHTMLDOMNode; IHTMLElement parentEl = cnode.parentNode as IHTMLElement; if (parentEl == null) { IHTMLElement frame = BrowserExtensions.GetFrame((IHTMLElement)el); PopulateNode(frame, dataDict); } else { PopulateNode(parentEl, dataDict); } } }
/// <summary> /// 获取当前节点的父树节点和DOM父节点,如果不存在则创建它 /// </summary> /// <param name="node"></param> /// <param name="parentTreeNode"></param> /// <param name="parentNode"></param> private void GetOrCreateParent(IHTMLDOMNode node, out TreeNode parentTreeNode, out IHTMLDOMNode parentNode) { parentTreeNode = null; parentNode = null; if (node == null) { return; } IHTMLDOMNode parent = node.parentNode; if ((parent.nodeName != "#document")) { parentTreeNode = GetTreeNode(parent); if (parentTreeNode == null) { parentTreeNode = NewDomNode(parent); } parentNode = parent; } else { //iframe node parent = BrowserExtensions.GetFrame((mshtml.IHTMLDocument2)parent) as IHTMLDOMNode; if (parent == null) { return; } //获取iframe树节点,如果不存在则创建它 parentTreeNode = GetTreeNode(parent); if (parentTreeNode == null) { parentTreeNode = NewDomNode(parent); } //检查当前节点是否是iframe的子节点,如果不是怎增加为子节点 TreeNode curNode = GetTreeNode(node); if (!parentTreeNode.Nodes.Contains(curNode)) { parentTreeNode.Nodes.Add(curNode); } GetOrCreateParent(parent, out parentTreeNode, out parentNode); } }
private void SetFrameList(IHTMLElement el) { List <IHTMLElement> frames = new List <IHTMLElement>(); IHTMLElement frame = BrowserExtensions.GetFrame(el); while (frame != null) { frames.Add(frame); frame = BrowserExtensions.GetFrame(frame); } foreach (var frameElement in frames) { FindAttribute attribute = GetFrameFinder((IHTMLElement)frameElement); if (attribute != null) { FrameList.Add(attribute); } } FrameList.Reverse(); }
private void InspectFrameMouseEvent(mshtml.IHTMLElementCollection fc) { if (fc == null) { return; } if (fc.length > 0) { for (int i = 0; i < fc.length; i++) { object id = (object)i; IHTMLWindow2 frameWindow = (IHTMLWindow2)fc.item(id, 0); mshtml.HTMLDocument frameDoc = (mshtml.HTMLDocument)frameWindow.document; mshtml.DispHTMLDocument frameDispDoc = (mshtml.DispHTMLDocument)frameDoc; DHTMLEventHandler onmousedownhand = new DHTMLEventHandler(frameDoc); onmousedownhand.Handler += new DHTMLEvent(Mouse_Down); frameDispDoc.onmousedown = onmousedownhand; IHTMLElementCollection col = BrowserExtensions.GetFrames((IHTMLDocument2)frameDoc); InspectFrameMouseEvent(col); } } }
protected async Task <bool> Post(string uri, object objectToSend, bool token = true) { if (objectToSend == null || uri == null) { return(false); } if (token && AccessToken == null) { UriHelper.NavigateTo("/authorize"); } var message = new HttpRequestMessage(HttpMethod.Post, uri); var content = new StringContent(Json.Serialize(objectToSend), System.Text.Encoding.UTF8, "application/json"); if (token) { content.Headers.TryAddWithoutValidation("Auth-Token", AccessToken); } message.Content = content; try { var response = await Http.SendAsync(message); if (response.StatusCode == HttpStatusCode.Unauthorized) { AccessToken = null; await BrowserExtensions.RemoveLocalStorageAsync(Defaults.AuthTokenStorageName); UriHelper.NavigateTo("/authorize"); } return(response.StatusCode == HttpStatusCode.OK); } catch { return(false); } }
public Task <string> Execute(string keyword) { return(Task.Run(() => { var encodedKeyword = HttpUtility.UrlEncode(keyword); string output = null; webDriver.Url = "https://www.google.com/search?q=" + encodedKeyword + "&num=100&ip=0.0.0.0&source_ip=0.0.0.0&ie=UTF-8&oe=UTF-8&hl=en&adtest=on&noj=1&igu=1&uule=w+CAIQIFISCQs2MuSEtepUEUK33kOSuTsc&nomo=1¬a=1&biw=1075&bih=5000&adsdiag=-6218766314297752031&" + time; var elements = BrowserExtensions.FindElements(webDriver, By.XPath("//a[contains(@ping, '/url') and @class='q']")); int i = 0; foreach (var element in elements) { if (i <= this.positions) { i++; var urlPart = element.GetAttribute("ping"); var match = Regex.Match(urlPart, "&url=(.*)&ved"); string result = match.Groups[1].Value; output += result + "\r\n"; } } Debug.WriteLine(output); return output; })); }