humanemulator.net — справка.
Продукт, демо и покупка — на
хуманэмулятор.рф
|
Демо
|
Купить
|
API
|
лог изменений
get_all_attributes_by_attribute
get_all_attributes_by_attribute($attr_name_need,$attr_name,$attr_value,$exactly,$frame=-1); - данная функция используется для получения массива значений заданного аттрибута у всех элементов страницы, находя их по заданному значению аттрибута или его части.Функция на вход принимает параметры:
С версии 4.6.41 достпуно: можно передавать вложенные фреймы, принцип такой же самый, передается строка с номерами фреймов, разделенных : например при передаче "1:0:5" - будет выбран фрейм с номером 1 в нем под фрейм с номером 0 и в нем подфрейм с номером 5
С версии 7.0.38 достпуно: можно передавать "url=>XXX", тогда будет произведен поиск фрейма, который содержит заданнй src, или передавать "name=>XXX" - тогда будет поиск фрейма, по заданной части имени.
После отработки функция возвращает результат своей работы в скрипт :
Пример использования
<?php // Scenario: Example of getting all attributes of elements by attribute $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"; // Example 1: Basic usage - get all attributes of elements by attribute $attrName = "name"; $attrValue = "mouseover1"; // Get all attributes of elements with attribute "name" equal to "mouseover1" $attributes = DOM::$element->get_all_attributes_by_attribute("id", $attrName, $attrValue, true); // Check if the operation was successful if ($attributes !== false) { echo "Successfully retrieved attributes for elements with attribute '$attrName'='$attrValue'\n"; } else { echo "Failed to retrieve attributes for elements with attribute '$attrName'='$attrValue'\n"; } echo "All attributes of elements with attribute '$attrName'='$attrValue':\n"; if (is_array($attributes) && count($attributes) > 0) { foreach ($attributes as $index => $attrArray) { echo "Element " . ($index + 1) . " attributes:\n"; if (is_array($attrArray) && count($attrArray) > 0) { foreach ($attrArray as $name => $value) { echo " $name: '$value'\n"; } } else { echo " No attributes found\n"; } echo "\n"; } } else { echo "No attributes found for elements with attribute '$attrName'='$attrValue'\n"; } echo "\n"; // Example 2: Get all attributes of elements by another attribute $attrName = "data-action"; $attrValue = "click"; // Get all attributes of elements with attribute "data-action" equal to "click" $attributes = DOM::$element->get_all_attributes_by_attribute("id", $attrName, $attrValue, true); // Check if the operation was successful if ($attributes !== false) { echo "Successfully retrieved attributes for elements with attribute '$attrName'='$attrValue'\n"; } else { echo "Failed to retrieve attributes for elements with attribute '$attrName'='$attrValue'\n"; } echo "All attributes of elements with attribute '$attrName'='$attrValue':\n"; if (is_array($attributes) && count($attributes) > 0) { foreach ($attributes as $index => $attrArray) { echo "Element " . ($index + 1) . " attributes:\n"; if (is_array($attrArray) && count($attrArray) > 0) { foreach ($attrArray as $name => $value) { echo " $name: '$value'\n"; } } else { echo " No attributes found\n"; } echo "\n"; } } else { echo "No attributes found for elements with attribute '$attrName'='$attrValue'\n"; } echo "\n"; // Example 3: Get all attributes of elements using partial match $attrName = "data-type"; $attrValue = "link"; // Get all attributes of elements with attribute "data-type" containing "link" $attributes = DOM::$element->get_all_attributes_by_attribute("id", $attrName, $attrValue, false); // Check if the operation was successful if ($attributes !== false) { echo "Successfully retrieved attributes for elements with attribute '$attrName' containing '$attrValue'\n"; } else { echo "Failed to retrieve attributes for elements with attribute '$attrName' containing '$attrValue'\n"; } echo "All attributes of elements with attribute '$attrName' containing '$attrValue':\n"; if (is_array($attributes) && count($attributes) > 0) { foreach ($attributes as $index => $attrArray) { echo "Element " . ($index + 1) . " attributes:\n"; if (is_array($attrArray) && count($attrArray) > 0) { foreach ($attrArray as $name => $value) { echo " $name: '$value'\n"; } } else { echo " No attributes found\n"; } echo "\n"; } } else { echo "No attributes found for elements with attribute '$attrName' containing '$attrValue'\n"; } echo "\n"; // Example 4: Get all attributes of elements with a specific attribute within a frame // This example assumes there is a frame with index 0 on the page $attrName = "id"; $attrValue = "frm1"; // Check if frame exists first $frameExists = DOM::$frame->is_exist_by_number(0); if ($frameExists) { echo "Frame 0 exists\n"; // Get all attributes of elements with attribute "id" equal to "frm1" within the first frame $attributes = DOM::$element->get_all_attributes_by_attribute("class", $attrName, $attrValue, true, "0"); // Check if the operation was successful if ($attributes !== false) { echo "Successfully retrieved attributes for elements with attribute '$attrName'='$attrValue' in frame 0\n"; } else { echo "Failed to retrieve attributes for elements with attribute '$attrName'='$attrValue' in frame 0\n"; } echo "All attributes of elements with attribute '$attrName'='$attrValue' in frame 0:\n"; if (is_array($attributes) && count($attributes) > 0) { foreach ($attributes as $index => $attrArray) { echo "Element " . ($index + 1) . " attributes:\n"; if (is_array($attrArray) && count($attrArray) > 0) { foreach ($attrArray as $name => $value) { echo " $name: '$value'\n"; } } else { echo " No attributes found\n"; } echo "\n"; } } else { echo "No attributes found for elements with attribute '$attrName'='$attrValue' in frame 0\n"; } echo "\n"; } else { echo "Frame 0 does not exist\n"; } // Example 5: Get all attributes of elements with a non-existent attribute $attrName = "nonexistent"; $attrValue = "value"; // Get all attributes of elements with a non-existent attribute $attributes = DOM::$element->get_all_attributes_by_attribute("id", $attrName, $attrValue, true); // Check if the operation was successful if ($attributes !== false) { echo "Unexpectedly retrieved attributes for elements with non-existent attribute '$attrName'='$attrValue'\n"; } else { echo "As expected, failed to retrieve attributes for elements with non-existent attribute '$attrName'='$attrValue'\n"; } echo "All attributes of elements with attribute '$attrName'='$attrValue':\n"; if (is_array($attributes) && count($attributes) > 0) { foreach ($attributes as $index => $attrArray) { echo "Element " . ($index + 1) . " attributes:\n"; if (is_array($attrArray) && count($attrArray) > 0) { foreach ($attrArray as $name => $value) { echo " $name: '$value'\n"; } } else { echo " No attributes found\n"; } echo "\n"; } } else { echo "No attributes found for elements with attribute '$attrName'='$attrValue' (as expected)\n"; } echo "\n"; // Example 6: Use the retrieved attributes to interact with the elements $attrName = "data-status"; $attrValue = "disabled"; // Get all attributes of elements with attribute "data-status" equal to "disabled" $attributes = DOM::$element->get_all_attributes_by_attribute("id", $attrName, $attrValue, true); // Check if the operation was successful if ($attributes !== false) { echo "Successfully retrieved attributes for elements with attribute '$attrName'='$attrValue'\n"; } else { echo "Failed to retrieve attributes for elements with attribute '$attrName'='$attrValue'\n"; } echo "Interacting with elements that have attribute '$attrName'='$attrValue':\n"; if (is_array($attributes) && count($attributes) > 0) { // Get all numbers of elements with the same attribute $numbers = DOM::$element->get_all_numbers_by_attribute($attrName, $attrValue, true); // Check if the operation was successful if ($numbers !== false) { echo "Successfully retrieved numbers of elements with attribute '$attrName'='$attrValue'\n"; } else { echo "Failed to retrieve numbers of elements with attribute '$attrName'='$attrValue'\n"; } if (is_array($numbers) && count($numbers) > 0) { foreach ($numbers as $index => $number) { // Get the element by number $findedElement = DOM::$element->get_by_number($number); // Check if the operation was successful if ($findedElement !== false) { echo "Successfully retrieved element number $number\n"; if ($findedElement->is_exist()) { echo "- Element number $number exists.\n"; // Display the attributes if (isset($attributes[$index]) && is_array($attributes[$index])) { echo " Attributes:\n"; foreach ($attributes[$index] as $name => $value) { echo " $name: '$value'\n"; } } else { echo " No attributes found\n"; } echo "\n"; } else { echo "- Element number $number does not exist\n"; } } else { echo "Failed to retrieve element number $number\n"; } } } else { echo "No elements found with attribute '$attrName'='$attrValue'\n"; } } else { echo "No attributes found for elements with attribute '$attrName'='$attrValue'\n"; } echo "\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:7010" from xweb_human_emulator import * # начало echo("<hr><font color=blue>common.get_all_attributes_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_attributes_by_attribute("href","name","onmouseover",true)) # 3 echo("3. Получим href всех элементов с атрибутом, содержащим заданное значение : ") print(anchor.get_all_attributes_by_attribute("href","name","on",false)) # 4 echo("4. Получим href всех элементов несуществующего элемента : ") if not anchor.get_all_attributes_by_attribute("href","name","ptych",false): echo("element not exist\n") # 5 echo("5. Получим href всех элементов с заданным значением атрибута в нулевом фрейме : ") print(anchor.get_all_attributes_by_attribute("href","name","onmouseover",true,0)) # 6 echo("6. Получим href всех элементов с атрибутом, содержащим заданное значение в нулевом фрейме : ") print(anchor.get_all_attributes_by_attribute("href","name","on",false,0)) # 7 echo("7. Получим href всех элементов несуществующего элемента в нулевом фрейме : ") if not anchor.get_all_attributes_by_attribute("href","name","ptych",false,0): echo("element not exist\n") # 8 echo("8. Получим href всех элементов в несуществующем фрейме : ") if not anchor.get_all_attributes_by_attribute("href","name","o",false,105000): echo("frame not exist") # конец 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_attributes_by_attribute</font><hr>"); // 1 шаг echo("1. Перейдем на полигон : "); echo(browser.navigate("rpa-bot.ru/wiki/poligon/anchor.html")+"<br>"); // 2 шаг echo("2. Выведем массив с href анкоров, содержащих в name 'ссылка' : "); Console.Write(anchor.get_all_attributes_by_attribute("href","name","l")+"<br>"); // 3 шаг echo("3. Выведем массив с href анкоров, содержащих в name 'on' в нулевом фрейме : "); Console.Write(anchor.get_all_attributes_by_attribute("href","name","on",false,"0")); // конец 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_attributes_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_attributes_by_attribute("href","name","onmouseover",true)); // 3 шаг echo("\n3. Получим href всех элементов с атрибутом, содрежащим заданное значение :\n\n"); echo(anchor.get_all_attributes_by_attribute("href","name","on",false)); // 4 шаг echo("\n4. Получим href всех элементов несуществующего элемента : "); if (!anchor.get_all_attributes_by_attribute("href","name","ptych",false)) echo("element not exist\n"); // 5 шаг echo("\n5. Получим href всех элементов с заданным значением атрибута в нулевом фрейме : \n\n"); echo(anchor.get_all_attributes_by_attribute("href","name","onmouseover",true,0)); // 6 шаг echo("\n6. Получим href всех элементов с атрибутом, содрежащим заданное значение в нулевом фрейме : \n\n"); echo(anchor.get_all_attributes_by_attribute("href","name","on",false,0)); // 7 шаг echo("\n7. Получим href всех элементов несуществующего элемента в нулевом фреме : "); if (!anchor.get_all_attributes_by_attribute("href","name","ptych",false,0)) echo("element not exist\n"); // 8 шаг echo("8. Получим href всех элементов в несуществующем фреме : "); if (!anchor.get_all_attributes_by_attribute("href","name","o",false,105000)) echo("frame not exist"); // конец echo("\n"); // Quit app.quit();
=============================================
Общие функции Объекты DOM System Vision Web Window
=============================================
Если что-то непонятно или необходимо узнать или считаете что надо добавить по работе этой функции, пишите в комментарии или на наш форум.
PS: В тестовом примере приведена работа с конкретным объектом DOM, но данная функция есть во всех DOM объектах, т.е она существует у всех объектов из категории DOM.