private bool GetCategoryGoodsItems(int pageIndex) { bool hasNewElement = false; ReadOnlyCollection <AndroidElement> allLinearLayouts = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.View/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout", true); foreach (AppiumWebElement element in allLinearLayouts) { ReadOnlyCollection <AppiumWebElement> imageElements = element.FindElementsByClassName("android.widget.ImageView"); ReadOnlyCollection <AppiumWebElement> textElements = element.FindElementsByClassName("android.widget.TextView"); if (imageElements != null && imageElements.Count > 0 && textElements != null && textElements.Count >= 2) { AppiumWebElement nameElement = textElements[textElements.Count - 2]; AppiumWebElement priceElement = textElements[textElements.Count - 1]; string id = nameElement.Text + priceElement.Text; if (!GoodsElementList.Exist(id) && !nameElement.Text.StartsWith("¥") && priceElement.Text.StartsWith("¥")) { Point location = new Point(element.Location.X, element.Location.Y); NcpAppElement appElement = GoodsElementList.Add(id, nameElement.Text, "", location, element.Size); appElement.Attributes.Add("price", priceElement.Text.Substring(1)); hasNewElement = true; } } } if (!hasNewElement && hasNoneNewElementTime == 0) { hasNoneNewElementTime++; hasNewElement = true; } return(hasNewElement); }
private void GetAllCategoryGoodsInfo() { try { List <NcpAppElement> allCategory3Elements = GetNeedGrabCategoryInfoFromFile(); for (int i = 0; i < allCategory3Elements.Count; i++) { NcpAppElement category3Element = allCategory3Elements[i]; string goodsFilePath = this.GetCategoryGoodsFilePath(category3Element.Id); if (!File.Exists(goodsFilePath)) { SelectCategory(category3Element); GetAllGoodsInCategory(category3Element); ClickBackButton(); } } } catch (Exception ex) { throw new Exception("获取店铺的所有分类的商品信息失败.", ex); } }
private List <NcpAppElement> GetNeedGrabCategoryInfoFromFile(string locationName) { string categoryFilePath = this.GetCategoryFilePath(locationName); ExcelReader er = new ExcelReader(categoryFilePath, "List"); int rowCount = er.GetRowCount(); List <NcpAppElement> allCategoryElements = new List <NcpAppElement>(); for (int i = 0; i < rowCount; i++) { Dictionary <string, string> row = er.GetFieldValues(i); string id = row["id"]; string category1Name = row["category1Name"]; string category2Name = row["category2Name"]; string needGrab = row["needGrab"]; string goodsCount = row["goodsCount"]; if (needGrab == "是") { string name = CommonUtil.IsNullOrBlank(category2Name) ? category1Name : category2Name; string parentCategory = CommonUtil.IsNullOrBlank(category2Name) ? "" : category1Name; NcpAppElement element = new NcpAppElement(); element.Id = id; element.Name = name; element.Attributes.Add("count", goodsCount); element.Attributes.Add("parentCategory", parentCategory); allCategoryElements.Add(element); } } return(allCategoryElements); }
private void SelectCategory(NcpAppElement categoryElement) { AndroidElement categoryMenuElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "分类" }, true, true, true); categoryMenuElement.Click(); AppiumWebElement cagtegoryElement = this.GetCategoryElement(categoryElement.Name, categoryElement.Attributes["count"]); cagtegoryElement.Click(); }
private void GetAllGoodsInCategory(NcpAppElement category3Element) { try { hasNoneNewElementTime = 0; GoodsElementList = new NcpAppElementList(); Size winSize = AppAccess.GetWindowSize(); AppAccess.SwipeDisplayElements(new Point(300, 1000), new Point(300, 300), 1000, 4000, GetCategoryGoodsItems); //保存到文件 string[] goodsColumns = new string[] { "name", "price", "unit", "category1Name", "category2Name", "category3Name" }; Dictionary <string, int> goodsColumnDic = CommonUtil.InitStringIndexDic(goodsColumns); string goodsFilePath = GetCategoryGoodsFilePath(category3Element.Id); ExcelWriter goodsEW = new ExcelWriter(goodsFilePath, "List", goodsColumnDic, null); string category1Name = category3Element.Attributes["category1Name"]; string category2Name = category3Element.Attributes["category2Name"]; string category3Name = category3Element.Name; for (int i = 0; i < GoodsElementList.Count; i++) { NcpAppElement element = GoodsElementList[i]; string name = element.Name; string price = element.Attributes["price"]; string unit = element.Attributes["unit"]; Dictionary <string, string> row = new Dictionary <string, string>(); row.Add("name", name); row.Add("price", price); row.Add("unit", unit); row.Add("category1Name", category1Name); row.Add("category2Name", category2Name); row.Add("category3Name", category3Name); goodsEW.AddRow(row); } goodsEW.SaveToDisk(); } catch (Exception ex) { throw new Exception("获取商品信息失败, categoryId = " + category3Element.Id, ex); } }
private bool GetCategoryItems(int pageIndex) { int pageOffset = pageIndex * 2000; bool hasNewElement = false; ReadOnlyCollection <AndroidElement> allRelativeLayouts = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout", true); foreach (AppiumWebElement element in allRelativeLayouts) { ReadOnlyCollection <AppiumWebElement> textElements = element.FindElementsByClassName("android.widget.TextView"); if (textElements != null && textElements.Count >= 2) { //需要改造,将名称+宽度作为唯一标识,因为商品数量经常变 //获取某类下的所有商品时,商品数量要按抓取此分类时的数量为准 //增加店铺名的抓取 AppiumWebElement nameElement = textElements[0]; AppiumWebElement countElement = textElements[1]; string id = nameElement.Text + countElement.Text; if (!_CategoryElementList.Exist(id)) { Point location = new Point(element.Location.X, element.Location.Y + pageOffset); NcpAppElement appElement = CategoryElementList.Add(id, nameElement.Text, "Level1", location, element.Size); appElement.Attributes.Add("count", countElement.Text.Substring(1, countElement.Text.Length - 2)); hasNewElement = true; } } } ReadOnlyCollection <AndroidElement> allLinearLayouts = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout", true); foreach (AppiumWebElement element in allLinearLayouts) { ReadOnlyCollection <AppiumWebElement> textElements = element.FindElementsByClassName("android.widget.TextView"); if (textElements != null && textElements.Count >= 2) { AppiumWebElement nameElement = textElements[0]; AppiumWebElement countElement = textElements[1]; string id = nameElement.Text + countElement.Text; if (!_CategoryElementList.Exist(id)) { Point location = new Point(element.Location.X, element.Location.Y + pageOffset); NcpAppElement appElement = CategoryElementList.Add(id, nameElement.Text, "Level2", location, element.Size); appElement.Attributes.Add("count", countElement.Text.Substring(1, countElement.Text.Length - 2)); hasNewElement = true; } } } return(hasNewElement); }
private NcpAppElementList GetCategory1Items() { NcpAppElementList allC1Elements = new NcpAppElementList(); //ReadOnlyCollection<AndroidElement> allC1Nodes = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.ListView/android.widget.RelativeLayout/android.widget.TextView[1]", true); ReadOnlyCollection <AndroidElement> allC1Nodes = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.ListView/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.TextView[1]", true); foreach (AppiumWebElement c1Node in allC1Nodes) { NcpAppElement c1Element = new NcpAppElement(); c1Element.Name = c1Node.Text; c1Element.Id = c1Element.Name; allC1Elements.Add(c1Element); } return(allC1Elements); }
private void GetAllGoodsInCategory(string locationName, NcpAppElement categoryElement) { try { hasNoneNewElementTime = 0; GoodsElementList = new NcpAppElementList(); Size winSize = AppAccess.GetWindowSize(); AppAccess.SwipeDisplayElements(new Point(winSize.Width - 20, winSize.Height - 200), new Point(winSize.Width - 20, 400), 1000, 10000, GetCategoryGoodsItems); //保存到文件 int goodsCount = int.Parse(categoryElement.Attributes["count"]); string[] goodsColumns = new string[] { "id", "name", "price", "categoryName", "locationName" }; Dictionary <string, int> goodsColumnDic = CommonUtil.InitStringIndexDic(goodsColumns); string goodsFilePath = (goodsCount == GoodsElementList.Count ? this.GetCategoryGoodsFilePath(locationName, categoryElement.Id) : this.GetCategoryGoodsFilePath("_Error_" + locationName, categoryElement.Id)); ExcelWriter goodsEW = new ExcelWriter(goodsFilePath, "List", goodsColumnDic, null); for (int i = 0; i < GoodsElementList.Count; i++) { NcpAppElement element = GoodsElementList[i]; string id = element.Id; string name = element.Name; string price = element.Attributes["price"]; Dictionary <string, string> row = new Dictionary <string, string>(); row.Add("id", element.Id); row.Add("name", name); row.Add("categoryName", categoryElement.Name); row.Add("price", price); row.Add("locationName", locationName); goodsEW.AddRow(row); } goodsEW.SaveToDisk(); } catch (Exception ex) { throw new Exception("获取商品信息失败, locationName = " + locationName + ", categoryId = " + categoryElement.Id, ex); } }
private void SelectCategory(NcpAppElement categoryElement) { string category1Name = categoryElement.Attributes["category1Name"]; AndroidElement c1Node = AppAccess.GetElementByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.ListView/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.TextView[1]", new string[] { category1Name }, true); c1Node.Click(); Thread.Sleep(1500); List <AppiumWebElement> cElements = this.GetCategoryElement(categoryElement.Name); AppiumWebElement cElement = cElements[cElements.Count - 1]; Point point = cElement.Location; Size size = cElement.Size; //点击文字上方的图标 AppAccess.Tap(1, point.X + size.Width / 2, point.Y - 80, 100); //cagtegoryElement.Click(); }
private bool GetCategoryGoodsItems(int pageIndex) { bool hasNewElement = false; List <XmlNode> allGoodsParentNodes = AppAccess.GetXmlElementsByXPath(new string[] { "//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.GridView/android.widget.FrameLayout", "//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.ListView/android.widget.GridView/android.widget.FrameLayout", "//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.GridView/android.widget.FrameLayout" }, false); if (allGoodsParentNodes != null) { foreach (XmlNode goodsParentNode in allGoodsParentNodes) { XmlNodeList goodsInfoNodes = goodsParentNode.SelectNodes("./android.widget.RelativeLayout/android.widget.TextView"); if (goodsInfoNodes.Count >= 3) { string nameValue = goodsInfoNodes[goodsInfoNodes.Count - 3].Attributes["text"].Value; string priceValue = goodsInfoNodes[goodsInfoNodes.Count - 2].Attributes["text"].Value; string unitValue = goodsInfoNodes[goodsInfoNodes.Count - 1].Attributes["text"].Value; if (!GoodsElementList.Exist(nameValue) && priceValue.StartsWith("¥")) { NcpAppElement appElement = GoodsElementList.Add(nameValue, nameValue, ""); decimal price = decimal.Parse(priceValue.Substring(1).Trim()); appElement.Attributes.Add("price", price.ToString()); appElement.Attributes.Add("unit", unitValue.Substring(1)); hasNewElement = true; } } } } if (!hasNewElement && hasNoneNewElementTime < 2) { hasNoneNewElementTime++; hasNewElement = true; Thread.Sleep(2000); } else { hasNoneNewElementTime = 0; } return(hasNewElement); }
private List <NcpAppElement> GetNeedGrabCategoryInfoFromFile() { string categoryFilePath = this.GetCategoryFilePath(); ExcelReader er = new ExcelReader(categoryFilePath, "List"); int rowCount = er.GetRowCount(); List <NcpAppElement> allCategory3Elements = new List <NcpAppElement>(); for (int i = 0; i < rowCount; i++) { Dictionary <string, string> row = er.GetFieldValues(i); string category1Name = row["category1Name"]; string category2Name = row["category2Name"]; string category3Name = row["category3Name"]; NcpAppElement element = new NcpAppElement(); element.Id = category1Name + "_" + category2Name + "_" + category3Name; element.Name = category3Name; element.Attributes.Add("category2Name", category2Name); element.Attributes.Add("category1Name", category1Name); allCategory3Elements.Add(element); } return(allCategory3Elements); }
private void GetAllCategoryGoodsInfo(string locationName) { try { List <NcpAppElement> needGrabCategoryElements = GetNeedGrabCategoryInfoFromFile(locationName); for (int i = 0; i < needGrabCategoryElements.Count; i++) { NcpAppElement categoryElement = needGrabCategoryElements[i]; string goodsFilePath = this.GetCategoryGoodsFilePath(locationName, categoryElement.Id); if (!File.Exists(goodsFilePath)) { SelectCategory(categoryElement); GetAllGoodsInCategory(locationName, categoryElement); } } } catch (Exception ex) { throw new Exception("获取店铺的所有分类的商品信息失败.", ex); } }
private bool GetSubCategoryItems(int pageIndex) { int pageOffset = pageIndex * 2000; bool hasNewElement = false; XmlNodeList allC2ParentNodes = AppAccess.GetXmlElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout", true); foreach (XmlNode c2ParentNode in allC2ParentNodes) { XmlNode c2Node = c2ParentNode.SelectSingleNode("./android.widget.LinearLayout/android.widget.TextView"); if (c2Node != null) { string text = c2Node.Attributes["text"].Value; string id = text + "Level2"; if (!_CategoryElementList.Exist(id)) { NcpAppElement appElement = CategoryElementList.Add(id, text, "Level2"); hasNewElement = true; } } XmlNodeList allC3Nodes = c2ParentNode.SelectNodes("./android.widget.LinearLayout/android.widget.GridView/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView"); if (allC3Nodes != null) { foreach (XmlNode c3Node in allC3Nodes) { string text = c3Node.Attributes["text"].Value; string id = text + "Level3"; if (!_CategoryElementList.Exist(id)) { NcpAppElement appElement = CategoryElementList.Add(id, text, "Level3"); hasNewElement = true; } } } } return(hasNewElement); }
private void GetCategoryInfo(string locationName) { try { AndroidElement categoryMenuElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "分类" }, true, true, true); categoryMenuElement.Click(); CategoryElementList = new NcpAppElementList(); Size winSize = AppAccess.GetWindowSize(); AppAccess.SwipeDisplayElements(new Point(winSize.Width - 20, winSize.Height - 20), new Point(winSize.Width - 20, 200), 2000, 5000, GetCategoryItems); NcpAppElementList sortedCategoryElements = CategoryElementList.SortByPosition(); //分级处理 List <NcpAppElement> level1Elements = new List <NcpAppElement>(); NcpAppElement lastLevel1Element = null; for (int i = 1; i < sortedCategoryElements.Count; i++) { NcpAppElement element = sortedCategoryElements[i]; if (element.TypeName == "Level1") { level1Elements.Add(element); lastLevel1Element = element; } else { lastLevel1Element.Children.Add(element); } } //保存到文件 string[] categoryColumns = new string[] { "id", "category1Name", "category2Name", "needGrab", "goodsCount", "locationName" }; Dictionary <string, int> categoryColumnDic = CommonUtil.InitStringIndexDic(categoryColumns); string categoryFilePath = this.GetCategoryFilePath(locationName); ExcelWriter categoryEW = new ExcelWriter(categoryFilePath, "List", categoryColumnDic, null); for (int i = 0; i < level1Elements.Count; i++) { NcpAppElement element = level1Elements[i]; string category1Name = element.Name; string needGrab = element.Children.Count == 0 ? "是" : "否"; string goodsCount = element.Attributes["count"]; Dictionary <string, string> row = new Dictionary <string, string>(); row.Add("id", element.Id); row.Add("category1Name", category1Name); row.Add("category2Name", ""); row.Add("needGrab", needGrab); row.Add("goodsCount", goodsCount); row.Add("locationName", locationName); categoryEW.AddRow(row); if (element.Children.Count > 0) { for (int j = 0; j < element.Children.Count; j++) { NcpAppElement childElement = element.Children[j]; string category2Name = childElement.Name; string childGoodsCount = childElement.Attributes["count"]; Dictionary <string, string> childRow = new Dictionary <string, string>(); childRow.Add("id", childElement.Id); childRow.Add("category1Name", category1Name); childRow.Add("category2Name", category2Name); childRow.Add("needGrab", "是"); childRow.Add("goodsCount", childGoodsCount); childRow.Add("locationName", locationName); categoryEW.AddRow(childRow); } } } categoryEW.SaveToDisk(); } catch (Exception ex) { throw new Exception("获取分类信息失败.", ex); } }
private void GetCategoryInfo() { string categoryFilePath = this.GetCategoryFilePath(); if (!File.Exists(categoryFilePath)) { try { //获取一级分类信息 NcpAppElementList allC1Elements = this.GetCategory1Items(); foreach (NcpAppElement c1Element in allC1Elements) { //AndroidElement c1Node = AppAccess.GetElementByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.ListView/android.widget.RelativeLayout/android.widget.TextView[1]", new string[] { c1Element.Name }, true); AndroidElement c1Node = AppAccess.GetElementByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.ListView/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.TextView[1]", new string[] { c1Element.Name }, true); c1Node.Click(); CategoryElementList = new NcpAppElementList(); Size winSize = AppAccess.GetWindowSize(); AppAccess.SwipeDisplayElements(new Point(300, 1000), new Point(300, 300), 2000, 2000, GetSubCategoryItems); //分级处理 List <NcpAppElement> level2Elements = new List <NcpAppElement>(); NcpAppElement lastLevel2Element = null; for (int i = 0; i < CategoryElementList.Count; i++) { NcpAppElement element = CategoryElementList[i]; if (element.TypeName == "Level2") { level2Elements.Add(element); lastLevel2Element = element; } else { lastLevel2Element.Children.Add(element); } } c1Element.Children.AddRange(level2Elements); } //保存到文件 string[] categoryColumns = new string[] { "category1Name", "category2Name", "category3Name" }; Dictionary <string, int> categoryColumnDic = CommonUtil.InitStringIndexDic(categoryColumns); ExcelWriter categoryEW = new ExcelWriter(categoryFilePath, "List", categoryColumnDic, null); for (int i = 0; i < allC1Elements.Count; i++) { NcpAppElement c1Element = allC1Elements[i]; string category1Name = c1Element.Name; if (c1Element.Children.Count > 0) { for (int j = 0; j < c1Element.Children.Count; j++) { NcpAppElement c2Element = c1Element.Children[j]; string category2Name = c2Element.Name; if (c2Element.Children.Count > 0) { for (int k = 0; k < c2Element.Children.Count; k++) { NcpAppElement c3Element = c2Element.Children[k]; string category3Name = c3Element.Name; Dictionary <string, string> row = new Dictionary <string, string>(); row.Add("category1Name", category1Name); row.Add("category2Name", category2Name); row.Add("category3Name", category3Name); categoryEW.AddRow(row); } } } } } categoryEW.SaveToDisk(); } catch (Exception ex) { throw new Exception("获取分类信息失败.", ex); } } }