XPath universal C# step for work with elements

Discussion in 'Useful articles' started by LightWood, Apr 2, 2017.

  1. LightWood

    LightWood Moderator

    Joined:
    Nov 4, 2010
    Messages:
    848
    Likes Received:
    328
    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:
    Code (csharp):
    1. String xpath_exp = "//select[@id='lang-chooser']/option[@selected='selected'][contains(.,'United States')]";
    2. String action_ev = "rise|focus";
    3. String set_action = "";
    4. 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
     

    Attached Files:

    PHaRTnONu likes this.

Пользователи просматривающие тему (Пользователей: 0, Гостей: 0)