humanemulator.net — справка.
Продукт, демо и покупка — на
хуманэмулятор.рф
|
Демо
|
Купить
|
API
|
лог изменений
get_all_by_attribute
get_all_by_attribute($attr_name,$attr_value,$exactly=false,$frame=-1); - данная функция используется для получения списка DOM интерфейсов элементов страницы, находя их по заданному значению аттрибута или его части.Доступна с версии 4.6.34
Функция на вход принимает параметры:
С версии 4.6.41 достпуно: можно передавать вложенные фреймы, принцип такой же самый, передается строка с номерами фреймов, разделенных : например при передаче "1:0:5" - будет выбран фрейм с номером 1 в нем под фрейм с номером 0 и в нем подфрейм с номером 5
С версии 7.0.38 достпуно: можно передавать "url=>XXX", тогда будет произведен поиск фрейма, который содержит заданнй src, или передавать "name=>XXX" - тогда будет поиск фрейма, по заданной части имени.
После отработки функция возвращает результат своей работы в скрипт :
Пример использования
<?php // Scenario: Examples of using get_all_by_attribute function to retrieve multiple DOM elements by their attributes // Path to init.php file for connecting to XHE API $xhe_host = "localhost:7010"; if (!isset($path)){ // Path to the init.php file for connecting to the XHE API $path = "../../../Templates/init.php"; // Including init.php grants access to all classes and functionality for working with the XHE API require($path); } // Navigate to a webpage with various elements $navigateResult = WEB::$browser->navigate(TEST_POLYGON_URL . "anchor.html"); // Check if navigation was successful if ($navigateResult) { echo "Successfully navigated to anchor.html\n"; // Wait for the page to load $waitResult = WEB::$browser->wait_for(); if ($waitResult) { echo "Page loaded successfully\n"; echo "\n\n=== Examples of using get_all_by_attribute function ===\n\n"; // Example 1: Get elements by exact attribute match $attr_name = "name"; $attr_value = "mouseover1"; $elements = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, true); // Check if the operation was successful if ($elements->get_count() > 0) { echo "Successfully retrieved elements with exact attribute '{$attr_name}'='{$attr_value}'\n"; // Get count of elements $count = $elements->get_count(); echo "\n\nTotal elements found with exact attribute '{$attr_name}'='{$attr_value}': {$count}"; // Example 2: Iterate through retrieved elements and display their details echo "\n\nIterating through elements with exact attribute '{$attr_name}'='{$attr_value}':"; for ($k = 0; $k < $count; $k++) { $targetElement = $elements->get($k); if ($targetElement !== false && $targetElement->is_exist()) { echo "\nElement #{$k}:"; $tag = $targetElement->get_tag(); if ($tag !== false) { echo "\n Tag: " . $tag; } else { echo "\n Failed to get tag"; } $name = $targetElement->get_name(); if ($name !== false) { echo "\n Name: " . $name; } else { echo "\n Failed to get name"; } $id = $targetElement->get_id(); if ($id !== false) { echo "\n ID: " . $id; } else { echo "\n Failed to get ID"; } $attr = $targetElement->get_attribute($attr_name); if ($attr !== false) { echo "\n Attribute '{$attr_name}': " . $attr; } else { echo "\n Failed to get attribute '{$attr_name}'"; } $value = $targetElement->get_value(); if ($value !== false) { echo "\n Value: " . $value; } else { echo "\n Failed to get value"; } } else { echo "\nFailed to get element #{$k}"; } } } else { echo "Failed to retrieve elements with exact attribute '{$attr_name}'='{$attr_value}'\n"; } // Example 3: Get elements by partial attribute match $attr_name = "data-action"; $attr_value = "click"; $elements = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, false); // exactly = false for partial match // Check if the operation was successful if ($elements !== false) { echo "\n\nSuccessfully retrieved elements with partial attribute '{$attr_name}' containing '{$attr_value}'\n"; // Get count of elements $count = $elements->get_count(); echo "\n\nTotal elements found with partial attribute '{$attr_name}' containing '{$attr_value}': {$count}"; // Example 4: Iterate through elements with partial attribute match echo "\n\nIterating through elements with partial attribute '{$attr_name}' containing '{$attr_value}':"; for ($k = 0; $k < $count; $k++) { $targetElement = $elements->get($k); if ($targetElement !== false && $targetElement->is_exist()) { echo "\nElement #{$k}:"; $tag = $targetElement->get_tag(); if ($tag !== false) { echo "\n Tag: " . $tag; } else { echo "\n Failed to get tag"; } $attr = $targetElement->get_attribute($attr_name); if ($attr !== false) { echo "\n Full attribute '{$attr_name}': " . $attr; } else { echo "\n Failed to get attribute '{$attr_name}'"; } } else { echo "\nFailed to get element #{$k}"; } } } else { echo "Failed to retrieve elements with partial attribute '{$attr_name}' containing '{$attr_value}'\n"; } // Example 5: Get elements by attribute and filter by tag $attr_name = "data-type"; $attr_value = "link"; $elements = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, true); // Check if the operation was successful if ($elements !== false) { echo "\n\nSuccessfully retrieved elements with attribute '{$attr_name}'='{$attr_value}' for filtering\n"; $linkElements = []; $otherElements = []; for ($k = 0; $k < $elements->get_count(); $k++) { $targetElement = $elements->get($k); if ($targetElement !== false && $targetElement->is_exist()) { $tag = $targetElement->get_tag(); if ($tag !== false) { if ($tag === "a") { $linkElements[] = $targetElement; $attr = $targetElement->get_attribute($attr_name); $innerText = $targetElement->get_inner_text(); if ($attr !== false && $innerText !== false) { echo "\nLink element found: {$attr_name}=" . $attr . ", text=" . $innerText; } else { echo "\nLink element found but failed to get attributes"; } } else { $otherElements[] = $targetElement; $attr = $targetElement->get_attribute($attr_name); $innerText = $targetElement->get_inner_text(); if ($attr !== false && $innerText !== false) { echo "\nOther element found: {$attr_name}=" . $attr . ", text=" . $innerText; } else { echo "\nOther element found but failed to get attributes"; } } } else { echo "\nFailed to get tag for element #{$k}"; } } else { echo "\nFailed to get element #{$k}"; } } echo "\n\nTotal link elements with attribute '{$attr_name}'='{$attr_value}': " . count($linkElements); echo "\nTotal other elements with attribute '{$attr_name}'='{$attr_value}': " . count($otherElements); } else { echo "Failed to retrieve elements with attribute '{$attr_name}'='{$attr_value}' for filtering\n"; } // Example 6: Get elements by attribute and interact with them $attr_name = "data-status"; $attr_value = "disabled"; $elements = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, true); // Check if the operation was successful if ($elements !== false) { echo "\n\nSuccessfully retrieved elements with attribute '{$attr_name}'='{$attr_value}' for interaction\n"; echo "\n\nInteracting with elements containing exact attribute '{$attr_name}'='{$attr_value}':"; for ($k = 0; $k < $elements->get_count(); $k++) { $targetElement = $elements->get($k); if ($targetElement !== false && $targetElement->is_exist()) { echo "\nElement #{$k}:"; $tag = $targetElement->get_tag(); if ($tag !== false) { echo "\n Tag: " . $tag; } else { echo "\n Failed to get tag"; } // Get element details $attr = $targetElement->get_attribute($attr_name); if ($attr !== false) { echo "\n Attribute '{$attr_name}': " . $attr; } else { echo "\n Failed to get attribute '{$attr_name}'"; } $innerText = $targetElement->get_inner_text(); if ($innerText !== false) { echo "\n Inner text: " . $innerText; } else { echo "\n Failed to get inner text"; } $name = $targetElement->get_name(); if ($name !== false) { echo "\n Name: " . $name; } else { echo "\n Failed to get name"; } $id = $targetElement->get_id(); if ($id !== false) { echo "\n ID: " . $id; } else { echo "\n Failed to get ID"; } } else { echo "\nFailed to get element #{$k}"; } } } else { echo "Failed to retrieve elements with attribute '{$attr_name}'='{$attr_value}' for interaction\n"; } // Example 7: Get elements by attribute in a specific frame (frame=0) $attr_name = "data-frame"; $attr_value = "main"; $elements = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, true, "0"); // Check if frame exists first $frameExists = DOM::$frame->is_exist_by_number(0); if ($frameExists) { echo "\n\nFrame 0 exists\n"; // Check if the operation was successful if ($elements !== false) { echo "Successfully retrieved elements with attribute '{$attr_name}'='{$attr_value}' in frame 0\n"; // Get count of elements in frame $count = $elements->get_count(); echo "\n\nTotal elements found with attribute '{$attr_name}'='{$attr_value}' in frame 0: {$count}"; } else { echo "Failed to retrieve elements with attribute '{$attr_name}'='{$attr_value}' in frame 0\n"; } } else { echo "\n\nFrame 0 does not exist\n"; } // Example 8: Get elements by custom data attribute $attr_name = "data-category"; $attr_value = "seo"; $elements = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, true); // Check if the operation was successful if ($elements !== false) { echo "\n\nSuccessfully retrieved elements with custom data attribute '{$attr_name}'='{$attr_value}'\n"; // Get count of elements $count = $elements->get_count(); echo "\n\nTotal elements found with custom data attribute '{$attr_name}'='{$attr_value}': {$count}"; } else { echo "Failed to retrieve elements with custom data attribute '{$attr_name}'='{$attr_value}'\n"; } // Example 9: Get elements by aria attribute $attr_name = "alt"; $attr_value = "Ссылка mouseover 1"; $elements = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, true); // Check if the operation was successful if ($elements !== false) { echo "\n\nSuccessfully retrieved elements with attribute '{$attr_name}'='{$attr_value}'\n"; echo "\n\nChecking attributes of elements with attribute '{$attr_name}'='{$attr_value}':"; for ($k = 0; $k < $elements->get_count(); $k++) { $targetElement = $elements->get($k); if ($targetElement !== false && $targetElement->is_exist()) { echo "\nElement #{$k}:"; $tag = $targetElement->get_tag(); if ($tag !== false) { echo "\n Tag: " . $tag; } else { echo "\n Failed to get tag"; } $attr = $targetElement->get_attribute($attr_name); if ($attr !== false) { echo "\n Attribute '{$attr_name}': " . $attr; } else { echo "\n Failed to get attribute '{$attr_name}'"; } $class = $targetElement->get_attribute("class"); if ($class !== false) { echo "\n Class: " . $class; } else { echo "\n Failed to get class"; } $title = $targetElement->get_attribute("title"); if ($title !== false) { echo "\n Title: " . $title; } else { echo "\n Failed to get title"; } } else { echo "\nFailed to get element #{$k}"; } } } else { echo "Failed to retrieve elements with attribute '{$attr_name}'='{$attr_value}'\n"; } // Example 10: Case sensitivity comparison $attr_name = "name"; $attr_value = "MOUSEOVER1"; $elementsExact = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, true); // Case-sensitive exact match $elementsPartial = DOM::$anchor->get_all_by_attribute($attr_name, $attr_value, false); // Case-insensitive partial match // Check if the operations were successful if ($elementsExact !== false && $elementsPartial !== false) { echo "\n\nCase sensitivity comparison for attribute '{$attr_name}'='{$attr_value}':"; echo "\nExact match (case-sensitive): " . $elementsExact->get_count() . " elements"; echo "\nPartial match (case-insensitive): " . $elementsPartial->get_count() . " elements"; } else { echo "Failed to retrieve elements for case sensitivity comparison\n"; } } else { echo "Failed to wait for page to load\n"; } } else { echo "Failed to navigate to anchor.html\n"; } // Quit the application WINDOW::$app->quit(); ?>
# Additional paths import sys sys.path.insert(0, '../../../Templates PY/') xhe_host = "127.0.0.1:7013" from xweb_human_emulator import * # начало echo("<hr><font color=blue>common.get_all_by_attribute</font><hr>") # 1 echo("1. Перейдем на полигон : ") echo(browser.navigate("https://rpa-bot.ru/wiki/poligon/anchor.html"),"<br>") # 2 echo("2. Получить внутренний текст всех ссылок с заданным значением href: ") print(anchor.get_all_by_attribute("href","#").get_inner_text()) # конец 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 = "localhost:7010"; InitXHE(); // начало echo("<hr><font color=blue>get_all_by_attribute</font><hr>"); // 1 шаг echo("1. Перейдем на полигон : "); echo(browser.navigate("rpa-bot.ru/wiki/poligon/anchor.html")+"<br>"); // 2 шаг echo("2. Получить внутренний текст всех ссылок с заданным значением href: \n"); echo(anchor.get_all_by_attribute("href","#").get_inner_text()); // конец echo("\n\n"); app.quit(); } }
// подключим функциональные объекты, если еще не подключен xhe_host="127.0.0.1:7010"; echo=require("../../../Templates JS/init.js"); // начало echo("<hr><font color=blue>anchor.get_all_by_attribute</font><hr>"); // 1 шаг echo("1. Перейдем на полигон : "); echo(browser.navigate("https://rpa-bot.ru/wiki/poligon/anchor.html")+"<br>"); // 2 шаг echo("2. Получить внутренний текст всех ссылок с заданным значением href : \n\n"); echo(anchor.get_all_by_attribute("href","#").get_inner_text()); // конец echo("\n"); // Quit app.quit();
=============================================
Общие функции Объекты DOM System Vision Web Window
=============================================
Если что-то непонятно или необходимо узнать или считаете что надо добавить по работе этой функции, пишите в комментарии или на наш форум.
PS: В тестовом примере приведена работа с конкретным объектом DOM, но данная функция есть во всех DOM объектах, т.е она существует у всех объектов из категории DOM.