XPath universal C# step for work with elements

LightWood

Moderator
Регистрация
04.11.2010
Сообщения
2 382
Благодарностей
915
Баллы
113
This code makes it very convenient to work with xpath from C# step, task of code is to replace the standard "cubes" with the more advanced method of searching for elements - xpath, with a convenient way.

Xpath technology is very flexible, in particular one of the popular applications (which it is difficult to do without it using standard ZP tools) - searching for items by the method of "finding an element, and inside it another element, and inside it a third ..." that is, a nested search when it is impossible to find unique feature by which you can immediately identify an item.

You can also for example find an adjacent element (which has a unique attribute), and from it "push off" to find the desired element (which does not have a unique search sign). In general, the scope for finding elements is simply the widest.

How does it work:
- The function is inserted into the "common code" block, this is the handler
- From the C# step, the "client" code is called, which executes what you need (click, take something, set the value, in general, get|set|rise)

What is the main convenience, here is the code in C# step:
C#:
String xpath_exp = "//select[@id='lang-chooser']/option[@selected='selected'][contains(.,'United States')]";
String action_ev = "rise|focus";
String set_action = "";
Return CommonCode.FindElementAndExecuteAction (instance, xpath_exp, action_ev, set_action);
Input parameters:
Xpath_exp - is the xpath expression for find the item
Action_ev - what we do with the element, possible options:

Argument 1:
-get- take the value
-set- set the value
-rise- execute JS event

Argument 2: what we do with the element, for example complex variants with examples:
Get|width - take the width of the found element
Set|value - set the value to an element, for example, in the text field of the text you need
Rise|click - click on an element

In general, everything is the same here as in the Action Designer.

Set_action - is used only in cases when set is made (for example set|value or there is set|style), that is, setting the value, in this case the specified text is written, that is what will be written in the value.

The cool things which are also implemented:
Rise|scroll - will get to the desired item
Set|selecteditems - selection of the drop-down menu, while it supports - Regex:your_text

Other information:
- The code works since version ZP 5.8.0.0
- Use the type of emulation specified in the project (in the settings, or you specified in the step)
- Now, for the time being, there is no way to recognize CAPTCHA in this way (no get|captcha is done)
- If an item is not found, then this step will fail with an error
- For making xpath expressions it is convenient to use the FireFox browser extension called Firepath (it is put as an addition to the Firebug add-on)
- The code was written by darkdiver at my request, for which he bowed low and much thanks
- Lessons on xpath can be found here http://zvon.org/xxl/XPathTutorial/Output/example1.html
- Now ProjectMaker have the XPath/JsonPath tester. This tool can be used to create proper XPath/JsonPath expression.

An example of using code in attachments.

Origin: Link
Author: CSS
Translate by: LightWood
 

Вложения

Регистрация
10.07.2017
Сообщения
29
Благодарностей
1
Баллы
3
Excellent. You made my day.
 

spyder

Client
Регистрация
19.10.2014
Сообщения
27
Благодарностей
1
Баллы
3
Thank you for your work.
 

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