how to get xpath from variable

pt001

Новичок
Регистрация
18.10.2018
Сообщения
5
Благодарностей
0
Баллы
1
as stated, i need to scrape from http get content in variable use xpath,howcan i set,thanks!
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 413
Благодарностей
5 900
Баллы
113

EtaLasquera

Client
Регистрация
02.01.2017
Сообщения
526
Благодарностей
112
Баллы
43
I like more the own code to retreive xpath:
Add own code settings to your project and add these lines before //insert your code here:
Код:
  public static HtmlElement FindByXpath(Instance instance, string xpath)
  {
  Tab tab = instance.ActiveTab;
  if ((tab.IsVoid) || (tab.IsNull)) throw new Exception("Guia encontrada");
  if (tab.IsBusy) tab.WaitDownloading();
   
  // find html element by tag
  HtmlElement he = tab.FindElementByXPath(xpath, 0);
  if (he.IsVoid || he.IsNull) throw new Exception("Objeto não encontrado");
  return he;
  }
   
  private static void WaitDownloading(Tab tab)
  {
  if (tab.IsBusy) tab.WaitDownloading();
  }
   
  public static string FindElementAndExecuteAction(Instance instance, string xpath, string action, string attrValue="")
  {
  Tab tab = instance.ActiveTab;
  if ((tab.IsVoid) || (tab.IsNull)) throw new Exception("Guia encontrada");
   
  var he = CommonCode.FindByXpath(instance, xpath);
  if (!action.Contains("|")) throw new ArgumentException ("Formato incorreto da ação");
  var tmp = action.ToLower().Split(new [] {'|'}, 2);
  var mainAction = tmp[0];
  var subAction = tmp[1];
   
  switch (mainAction)
  {
  case "get":
  switch(subAction)
  {
  case "value":
  string attributeValue = he.GetValue();
  if (string.IsNullOrEmpty(attributeValue))
  throw new Exception("Significado em branco");
  return attributeValue;
  case "selecteditems":
  string selectedItems = he.GetSelectedItems();
  if (string.IsNullOrEmpty(selectedItems))
  throw new Exception("SelectedItens não pode ser vazio");
  return selectedItems;
  default:
  string attributeText = he.GetAttribute(subAction);
  if (string.IsNullOrEmpty(attributeText))
  throw new Exception(string.Format("Atributo {0} em branco", subAction));
  return attributeText;
  }
  case "rise":
  switch(subAction)
  {
  case "scroll":
  he.ScrollIntoView();
  break;
  default:
  he.RiseEvent(subAction, instance.EmulationLevel);
  break;
  }
  WaitDownloading(tab);
  return "ok";
  case "set":
  switch(subAction)
  {
  case "value":
  he.SetValue(attrValue, instance.EmulationLevel, he.TagName=="select");
  break;
  case "selecteditems":
  const string selecteditems_regexp = "Regex:";
  if (attrValue.StartsWith(selecteditems_regexp))
  {
  attrValue = attrValue.Substring(selecteditems_regexp.Length, attrValue.Length - selecteditems_regexp.Length);
  var regex = new Regex(attrValue);
  var items = he.GetAttribute("items").Split(new [] { ";" }, StringSplitOptions.RemoveEmptyEntries);
  var matchedItems = string.Join(";", items.Where(item => regex.Match(item).Success));
  he.SetAttribute("selecteditems", matchedItems);  
  }
  else
  {
  he.SetSelectedItems(attrValue);
  }
  break;
  default:
  he.SetAttribute(subAction, attrValue);
  break;
  }
  WaitDownloading(tab);
  return "ok";
  default:
  throw new Exception ("Ação desconhecida");
  }
  return "ok";
  }
  }
To retreive xpath add the follow c# code:
Код:
string str = project.Variables["contadorPagina"].Value; //own str
string a = "(//div[@page='transacoesPage']/nav/ul/li/a[@class='ng-binding'][contains(.,"+str+")])"; //xpath
string b = "rise|click"; //action
string c =""; //null
return CommonCode.FindElementAndExecuteAction(instance, a, b, c);
 

pt001

Новичок
Регистрация
18.10.2018
Сообщения
5
Благодарностей
0
Баллы
1

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)