humanemulator.net — справка. Продукт, демо и покупка — на хуманэмулятор.рф | Демо | Купить | API | Changelog

get_by_attribute

get_by_attribute($attr_name,$attr_value,$exactly=true); - Получить интерфейс по атрибуту

Функция на вход принимает параметры:

  • $attr_name – название атрибута для выполнения поиска DOM элемента
  • $attr_value – значение атрибута для выполнения поиска DOM элемента
  • $exactly – Требуется точное соответствие значения атрибута? Да/Нет

    После выполнения функции возвращаемое значение будет равно одному из двух :
  • результат – интерфейс или false - если найти не удалось

    Пример использования
    <?php
    
    // Scenario: Get elements by attribute from a collection
    // Description: This example demonstrates how to get a collection of elements and then find specific elements by their attributes within that collection
    // Classes used: XHEInterfaces, XHEDiv, XHEBrowser, XHEApplication
    
    // Connection string to XHE API
    $xhe_host = getenv("RPABOT_HOST_URL");
    
    // Path to init.php file
    if (!isset($path))
    {
        // Path to init.php file for connecting to XHE API
        $path = "../../../Templates/init.php";
        // When connecting init.php file, all functionality of classes for working with XHE API will be available
        require($path);
    }
    
    // Navigate to the polygon page if the page was not loaded earlier
    WEB::$browser->navigate(TEST_POLYGON_URL . "divs.html");
    
    // Example: Get all div elements and then find specific divs by their attributes within that collection
    
    // Get all div elements on the page
    $divs = DOM::$div->get_all();
    
    // Check that we have found at least one div
    if ($divs->count() > 0)
    {
        echo "Found " . $divs->count() . " div elements\n";
        
        // Try to find a div with a specific ID attribute (exact match)
        $targetAttrName = "id";
        $targetAttrValue = "example";
        $divByAttr = $divs->get_by_attribute($targetAttrName, $targetAttrValue, true);
        
        // Check if the div with the specified attribute was found
        if ($divByAttr && $divByAttr->is_exist())
        {
            echo "\nFound div with attribute '" . $targetAttrName . "' = '" . $targetAttrValue . "' (exact match):\n";
            echo "Div inner text: " . $divByAttr->get_inner_text() . "\n";
            echo "Div inner number: " . $divByAttr->inner_number . "\n";
            
            // Get the class attribute of the found div
            $divClass = $divByAttr->get_attribute("class");
            echo "Div class: " . $divClass . "\n";
        }
        else
        {
            echo "\nNo div with attribute '" . $targetAttrName . "' = '" . $targetAttrValue . "' (exact match) found in the collection\n";
        }
        
        // Try to find a div with a partial class attribute match in div elements
        $partialAttrName = "class";
        $partialAttrValue = "example";
        $divByPartialAttr = $divs->get_by_attribute($partialAttrName, $partialAttrValue, false);
        
        // Check if the div with the partial attribute was found in div elements and exists
        if ($divByPartialAttr && $divByPartialAttr->is_exist())
        {
            echo "\nFound div with partial attribute '" . $partialAttrName . "' = '" . $partialAttrValue . "':\n";
            echo "Div inner text: " . $divByPartialAttr->get_inner_text() . "\n";
            echo "Div inner number: " . $divByPartialAttr->inner_number . "\n";
            echo "Full class attribute: " . $divByPartialAttr->get_attribute("class") . "\n";
    
        }
        else
        {
            echo "\nNo div with partial attribute '" . $partialAttrName . "' = '" . $partialAttrValue . "' found in the collection\n";
        }
    
    }
    else
    {
        echo "No div elements found on the page\n";
    }
    
    // Stop the application
    WINDOW::$app->quit();
    ?>
    # Additional paths
    import sys
    sys.path.insert(0, '../../../Templates PY/')
    
    xhe_host = "127.0.0.1:7010"
    from xweb_human_emulator import *
    
    # начало
    echo("<hr><font color=blue>interfaces.xxxxxxxxx</font><hr>")
    
    # 1 
    echo("1. Перейдем на полигон : ")
    echo(browser.navigate("https://rpa-bot.ru/wiki/poligon/anchor.html"),"<br>")
    
    # 2 
    echo("2. Получить все ссылки : ")
    anchors=anchor.get_all()
    echo(anchors.get_count(),"<br>")
    
    # 3 
    echo("3. Получим размеры ссылки с href = https://rpa-bot.ru/wiki/ : ")
    qstart_anchor=anchors.get_by_attribute("href","https://rpa-bot.ru/wiki/")
    echo(qstart_anchor.get_width()," x ")
    echo(qstart_anchor.get_height(),"<br>")
    
    # 4 
    echo("4. Получим размеры ссылки с текстом, содержащей note : ")
    note_anchor=anchors.get_by_attribute("href","note",false)
    echo(note_anchor.get_width()," x ")
    echo(note_anchor.get_height(),"<br>")
    
    # конец
    echo("<hr><br>")
    
    # Quit
    app.quit()
    #region using
    
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Threading;
    
    using XHE;
    using XHE.XHE_DOM;
    using XHE.XHE_System;
    using XHE.XHE_Window;
    using XHE.XHE_Web;
    
    #endregion
    
     class Program:XHEScript
     {
    	  static void Main(string[] args)
    	  {
    			// init XHE
    			server = Environment.GetEnvironmentVariable("RPABOT_HOST_URL");
    			InitXHE();
    
    			// начало
    			echo("<hr><font color=blue>interfaces.get_by_attribute</font><hr>");
    							
    			// 1 шаг
    			echo("1. Перейдем на полигон : ");
    			echo(browser.navigate("rpa-bot.ru/wiki/poligon/anchor.html")+"<br>");
    
    			// 2 шаг
    			echo("2. Получить все ссылки : ");
    			XHEInterfaces anchors=anchor.get_all();
    			echo(anchors.get_count()+"<br>");
    
    			// 3 шаг
    			echo("3. Получим размеры ссылки с href = https://rpa-bot.ru/wiki/ : ");
    			XHEInterface qstart_anchor=anchors.get_by_attribute("href","https://rpa-bot.ru/wiki/");
    			echo(qstart_anchor.get_width()+"x"+qstart_anchor.get_height()+"<br>");
    
    			// 4 шаг
    			echo("4. Получим размеры ссылки с текстом, содержащей note : ");
    			XHEInterface note_anchor=anchors.get_by_attribute("href","note",false);			
    			echo(note_anchor.get_width()+"x"+note_anchor.get_height()+"<br>");
    
    			// конец
    			echo("\n\n");
    
    			app.quit();            
    	  }
    }
    // подключим функциональные объекты, если еще не подключен
    xhe_host="127.0.0.1:7010";
    echo=require("../../../Templates JS/init.js");
    
    // начало
    echo("<hr><font color=blue>interface.get_by_attribute</font><hr>");
    
    // 1 шаг
    echo("1. Перейдем на полигон : ");
    echo(browser.navigate("https://rpa-bot.ru/wiki/poligon/anchor.html")+"<br>");
    
    // 2 шаг
    echo("2. Получить все ссылки : ");
    anchors=anchor.get_all();
    echo(anchors.get_count()+"<br>");
    
    // 3 шаг
    echo("3. Получим размеры ссылки с href = https://rpa-bot.ru/wiki/ : ");
    qstart_anchor=anchors.get_by_attribute("href","https://rpa-bot.ru/wiki/");
    echo(qstart_anchor.get_width()+" x "+qstart_anchor.get_height()+"<br>");
    
    // 4 шаг
    echo("4. Получим размеры ссылки с текстом, содержащей note : ");
    note_anchor=anchors.get_by_attribute("href","note",false);
    //anchors.__destruct();
    echo(note_anchor.get_width()+" x "+note_anchor.get_height()+"<br>");
    
    // конец
    echo("\n");
    
    // Quit
    app.quit();

    =============================================
    Interfaces    Объекты    DOM  System  Vision  Web  Window        
    =============================================
    Если что-то непонятно или необходимо узнать или считаете что надо добавить по работе этой функции, пишите в комментарии или на наш форум.