Что нового

[Автоматизация] Получение списка названий открытых вкладок в браузере Firefox

WSWR

AutoIT Гуру
Сообщения
941
Репутация
361
Попался на глаза пример автоматизации Firefox
http://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/page-2#entry1116572

Немного переделал, чтобы получать имена открытых вкладок, возможно, кому-то пригодится.

Код:
#include <CUIAutomation2.au3>
#include <UIAWrappers.au3>

Const $cFFNewTabByName = 'name:=Open a new tab'

$strFFExeFolder = 'C:\Program Files (x86)\Mozilla Firefox\'
$strFFStartup = ''
$strFFExe = $strFFExeFolder & 'firefox.exe '

$oFF = _UIA_getFirstObjectOfElementEx($oDesktop, 'class:=MozillaWindowClass', $treescope_children)

;$oFF.setfocus()

If IsObj($oFF) Then
	$oFFTabs = _UIA_getFirstObjectOfElementEx($oFF, 'controltype:=' & $UIA_TabControlTypeId, $treescope_subtree)
	$oFFTabsTring = _UIA_getObjectByFindAllEx($oFFTabs, $cFFNewTabByName, $treescope_subtree)
	ConsoleWrite($oFFTabsTring)
EndIf

Func _UIA_DumpThemAllEx($oElementStart, $treeScope)
	Dim $pCondition, $pTrueCondition
	Dim $pElements, $iLength
	Local $sTring = '', $sValue
	$objUIAutomation.CreateTrueCondition($pTrueCondition)
	$oCondition = ObjCreateInterface($pTrueCondition, $sIID_IUIAutomationCondition, $dtagIUIAutomationCondition)
	$oElementStart.FindAll($treeScope, $oCondition, $pElements)
	$oAutomationElementArray = ObjCreateInterface($pElements, $sIID_IUIAutomationElementArray, $dtagIUIAutomationElementArray)
	$oAutomationElementArray.Length($iLength)
	For $i = 0 To $iLength - 2
		$oAutomationElementArray.GetElement($i, $pUIElement)
		$oUIElement = ObjCreateInterface($pUIElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
		$sValue = _UIA_getPropertyValue($oUIElement, $UIA_NamePropertyId)
		If $sValue <> '' Then $sTring &= $sValue & @CRLF
	Next
	Return $sTring
EndFunc   ;==>_UIA_DumpThemAllEx

Func _UIA_getObjectByFindAllEx($obj, $str, $treeScope, $p1 = 0)
	Dim $pCondition, $pTrueCondition
	Dim $pElements, $iLength
	Local $tResult
	Local $propertyID
	Local $tPos
	Local $relPos
	Local $relIndex = 0
	Local $tMatch
	Local $tStr
	Local $properties2Match[1][2]
	$allProperties = StringSplit($str, ';', 1)
	$propertyCount = $allProperties[0]
	ReDim $properties2Match[$propertyCount][2]
	For $i = 1 To $allProperties[0]
		$tResult = StringSplit($allProperties[$i], ':=', 1)
		$tResult[1] = StringStripWS($tResult[1], 3)
		If $tResult[0] = 1 Then
			$propName = $UIA_NamePropertyId
			$propValue = $allProperties[$i]
			$properties2Match[$i - 1][0] = $propName
			$properties2Match[$i - 1][1] = $propValue
		Else
			$propName = $tResult[1]
			$propValue = $tResult[2]
			$bAdd = True
			If $propName = 'indexrelative' Then
				$relPos = $propValue
				$bAdd = False
			EndIf
			If ($propName = 'index') Or ($propName = 'instance') Then
				$relIndex = $propValue
				$bAdd = False
			EndIf
			If $bAdd = True Then
				$index = _UIA_getPropertyIndex($propName)
				Switch $propertiesSupportedArray[$index][1]
					Case $UIA_ControlTypePropertyId
						$propValue = Number(_UIA_getControlID($propValue))
				EndSwitch
				$properties2Match[$i - 1][0] = $propertiesSupportedArray[$index][1] ;~ store the propertyID (numeric value)
				$properties2Match[$i - 1][1] = $propValue
			EndIf
		EndIf
	Next
	$objUIAutomation.CreateTrueCondition($pTrueCondition)
	$oCondition = ObjCreateInterface($pTrueCondition, $sIID_IUIAutomationCondition, $dtagIUIAutomationCondition)
	$obj.FindAll($treeScope, $oCondition, $pElements)
	$oAutomationElementArray = ObjCreateInterface($pElements, $sIID_IUIAutomationElementArray, $dtagIUIAutomationElementArray)
	$matchCount = 0
	$oAutomationElementArray.Length($iLength)
	For $i = 0 To $iLength - 1
		$oAutomationElementArray.GetElement($i, $pUIElement)
		$oUIElement = ObjCreateInterface($pUIElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
		For $j = 0 To UBound($properties2Match, 1) - 1
			$propertyID = $properties2Match[$j][0]
			$propertyVal = $properties2Match[$j][1]
			Switch $propertyID
				Case $UIA_ControlTypePropertyId
					$propertyVal = Number($propertyVal)
			EndSwitch
			$propertyActualValue = _UIA_getPropertyValue($oUIElement, $propertyID)
			$tMatch = StringRegExp($propertyActualValue, $propertyVal, 0)
			If $tMatch = 0 Then ExitLoop
		Next
		If $tMatch = 1 Then
			If $relPos <> 0 Then
				$oAutomationElementArray.GetElement($i + $relPos, $pUIElement)
				$oUIElement = ObjCreateInterface($pUIElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
			EndIf
			If $relIndex <> 0 Then
				$matchCount = $matchCount + 1
				If $matchCount <> $relIndex Then $tMatch = 0
			EndIf
			If $tMatch = 1 Then
				If IsString($p1) Then
					_UIA_setVar('RTI.' & $p1, $oUIElement)
				EndIf
				Return $oUIElement
			EndIf
		EndIf
	Next
	Return _UIA_DumpThemAllEx($obj, $treeScope)
EndFunc   ;==>_UIA_getObjectByFindAllEx

Func _UIA_getFirstObjectOfElementEx($obj, $str, $treeScope)
	Local $tResult
	Local $pCondition
	Local $propertyID
	$tResult = StringSplit($str, ':=', 1)
	If $tResult[0] = 1 Then
		$propertyID = $UIA_NamePropertyId
		$tval = $str
	Else
		For $i = 0 To UBound($propertiesSupportedArray) - 1
			If $propertiesSupportedArray[$i][0] = StringLower($tResult[1]) Then
				$propertyID = $propertiesSupportedArray[$i][1]
				Switch $propertiesSupportedArray[$i][1]
					Case $UIA_ControlTypePropertyId
						$tval = Number($tResult[2])
					Case Else
						$tval = $tResult[2]
				EndSwitch
			EndIf
		Next
	EndIf
	$objUIAutomation.createPropertyCondition($propertyID, $tval, $pCondition)
	$oCondition = ObjCreateInterface($pCondition, $sIID_IUIAutomationPropertyCondition, $dtagIUIAutomationPropertyCondition)
	$iTry = 1
	$oUIElement = ''
	While Not IsObj($oUIElement) And $iTry <= $UIA_tryMax
		$t = $obj.Findfirst($treeScope, $oCondition, $pUIElement)
		$oUIElement = ObjCreateInterface($pUIElement, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
		If Not IsObj($oUIElement) Then
			Sleep(100)
			$iTry = $iTry + 1
		EndIf
	WEnd
	If IsObj($oUIElement) Then
		Return $oUIElement
	Else
		Return ''
	EndIf
EndFunc   ;==>_UIA_getFirstObjectOfElementEx

CUIAutomation2.au3 и UIAWrappers.au3 отсюда http://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/
 
Верх