Autoit - searching for a URL in a web page and click on it - autoit

I'm trying to figure out how can I click a URL that matches a specific pattern.
For example:
#include <IE.au3>
#include <MsgBoxConstants.au3>
local $pattern = "/123/"
Local $oIE = _IECreate("www.example.com",0,1,1,1)
Local $oLinks = _IELinkGetCollection($oIE)
For $oLink In $oLinks
If StringInStr($oLink, $pattern) Then
_IEAction($oLink, "click")
sleep(700)
_IEQuit($oIE)
ExitLoop
EndIf
Next
Basically what I need to achieve is,
if a $oLink in $oLinks contains $pattern - click on it.
The above program, for some reason, does not work.
Any suggestions?
Thank you

I'm not sure if you can use StringInStr on the object.
Try using:
StringInStr($oLink.href, $pattern)
instead.

Do you get your links with this piece of code?
#include <Array.au3>
#include <Inet.au3>
local $pattern = "/123/"
$source = _INetGetSource('https://www.nytimes.com/')
$links = StringRegExp($source, 'href="(http.*?)"', 3)
_ArrayDisplay($links)
Then you just need to adapt your For Next loop and use StringInStr on every links[$i]

Related

Conditional include of file

I'd like to include certain script only if it's present. Unfortunately #include is processed before the execution, so I can't make it conditional like this:
If FileExists(#ScriptDir & "\common.au3") Then
#include "common.au3"
EndIf
I tried to use Execute to evaluate the read file in place via Execute(ReadFile(...)). But that seems to only process single statements - I couldn't declare multiple functions for example.
Is there a different way to conditionally include another file?
Probably not a good design choice but if you really need to do somethin like this, try #OnAutoItStartRegister:
#OnAutoItStartRegister "_OnAutoItStart_CreateIncludes"
#include "include_collection.au3"
If IsDeclared("iExample_Common") Then
MsgBox(64, "", "Common.au3 exists")
Else
MsgBox(16, "", "Common.au3 wasnt included")
EndIf
MsgBox(0, "", "Your Script here")
Func _OnAutoItStart_CreateIncludes()
If StringInStr($CmdLineRaw, '-_OnAutoItStart_CreateIncludes', 1) Then Return
If FileExists(#ScriptDir & "\common.au3") And Not StringInStr(FileRead("include_collection.au3"), '#include "common.au3"') Then
FileWrite("include_collection.au3", '#include "common.au3"')
EndIf
$iPID = Run('"' & #AutoItExe & '" ' & $CmdLineRaw & ' -_OnAutoItStart_CreateIncludes', #WorkingDir, Default, 2)
While ProcessExists($iPID)
ConsoleWrite(StdoutRead($iPID))
Sleep(10)
WEnd
Exit
EndFunc ;==>_OnAutoItStart_CreateIncludes
Create an additional empty file "include_collection.au3" as well.
In this example, I created "commons.au3" containing a statement "$iExample_commons = 1234'.
Note: Once the file is included this way, it should not be deleted otherwise your script will fail again. This could probably be overcome too but at some point it will become very messy.
Maybe it's a better Idea to wrap a launcher around your application which will add/remove include lines before startup as needed.

Check if WinList() contains a certain title

I am listing all open windows using WinList() to get window title and -handle in AutoIt.
I want to check if resulting array contains a specific title. What is the best way to do this? There is no WinList().Contains("TitleName") or something like that.
Local $aList = WinList() ;Gets a list of Window Titles and IDs
OK, I got it now:
For $i = 1 To $aList[0][0]
If $aList[$i][0] = "Title/String you search for" Then
MsgBox($MB_SYSTEMMODAL, "", "MessageBox shows this text if title is in list.")
EndIf
Next
You could also use something similar to what you wrote.
#include <Array.au3>
Opt("WinDetectHiddenText", 0) ;0=don't detect, 1=do detect
Opt("WinSearchChildren", 0) ;0=no, 1=search children also
Opt("WinTextMatchMode", 1) ;1=complete, 2=quick
Opt("WinTitleMatchMode", 1) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
Local $title = 'AutoIt Help (v3.3.14.2)'
Local $aList = WinList()
;~ _ArrayDisplay($aList)
Local $iIndex = _ArraySearch($aList,$title)
WinActivate($aList[$iIndex][1], '')
Window exists?
"I am listing all open windows … I want to check if … contains a specific title. What is the best way to do this?"
As per Documentation - Function Reference - WinExists() :
Checks to see if a specified window exists.
Example.
Global Const $g_sWndTitle = 'Window title here'
If WinExists($g_sWndTitle) Then WinFlash($g_sWndTitle)
Retrieve window handle, -text and -title
Handle
"… to get window title and -handle …"
As per Documentation - Function Reference - WinGetHandle() :
Retrieves the internal handle of a window.
Example:
Global Const $g_sWndTitle = 'Window title here'
Global $g_hWnd
If WinExists($g_sWndTitle) Then
$g_hWnd = WinGetHandle($g_sWndTitle)
WinFlash($g_hWnd)
EndIf
Text
As per Documentation - Function Reference - WinGetText() :
Retrieves the text from a window.
Example:
Global Const $g_sWndTitle = 'Window title here'
If WinExists($g_sWndTitle) Then
WinFlash($g_sWndTitle)
ConsoleWrite(WinGetText($g_sWndTitle) & #CRLF)
EndIf
Title
Likewise, WinGetTitle().

How to Find the Windows default browser using autoit

i had tried following code and but iam getting null
$myDefaultBrowser = RegRead("HKEY_CURRENT_USER\Software\Clients\StartMenuInternet", "(Default)")
Here is another one:
#include <APIRegConstants.au3>
#include <WinAPIReg.au3>
ConsoleWrite(_getDefaultBrowser() & #crlf)
Func _getDefaultBrowser()
Return _WinAPI_AssocQueryString(".html", $ASSOCSTR_EXECUTABLE)
EndFunc ;==>_getDefaultBrowser
you can find the default browser by replacing "(Default)" with ""
$myDefaultBrowser = RegRead("HKEY_CURRENT_USER\Software\Clients\StartMenuInternet", "")

autoit IE click on a button in a website

I am trying to open a link by clicking on button inside a website.
This is the element :-
<div class="btn3">Like</div>
I tried this
$oBtn.classname = _IEGetObjById($oIE, "Like")
_IEAction($oBtn, "click")
not working. any help. Please.
You are doing it all wrong.
Try reading the help file.
This will work
Local $oInputs = _IETagNameGetCollection($oIE, "div")
For $oInput In $oInputs
If $oInput.classname == "btn3" Then _IEAction($oInput, "click")
Next
You can use $oBtn.classname only if you create an internet application object or an xml object with autoit. OR if you use _IETagNameGetCollection()
Here is an example:
#include <IE.au3>
Local $oIE = _IE_Example("form")
Local $oInputs = _IETagNameGetCollection($oIE, "input")
Local $oBtn
For $oInput In $oInputs
if $oInput.class = "btn3" Then
$oInput.Click
ExitLoop
Next
_IEQuit($oIE)
Of course you will need a little modification to the code since we know nothing about the website you are trying to automate

Can't retrieve links inside Frame

I am trying to use AutoIt to retrieve some data from this website:
http://www.acgme.org/adspublic/default.asp
Unfortunately, the page uses frames and I'm having trouble navigating to the page where the data is.
The link is "Accredited Programs"
#include <IE.au3>
$URL="http://www.acgme.org/adspublic/"
$MyIExplorer=_IECreate($URL,1,1,1,1)
Local $theFrame = _IEGetObjById($MyIExplorer,"control")
MsgBox(0,"The Frame",$theFrame.src)
Local $oLinks = _IELinkGetCollection($theFrame)
MsgBox(0, "Link Count", #extended & " links found")
When I run the code above, I am able to populate $theFrame with the correct frame object that houses the "Accredited Programs" link, but that's as far as I can get. The $oLinks collection comes back empty.
Frames are rather special. Use _IEFrameGetObjByName instead.
#include <IE.au3>
$URL="http://www.acgme.org/adspublic/"
$MyIExplorer=_IECreate($URL,1,1,1,1)
Local $theFrame = _IEFrameGetObjByName($MyIExplorer,"control")
Local $oLinks = _IELinkGetCollection($theFrame)
MsgBox(0, "Link Count", #extended & " links found")

Resources