AnAdventurer

Newbie with a question... or two (IE focused)

5 posts in this topic

#1 ·  Posted (edited)

Hello hello!

As the title suggests, I am fairly new to AutoIt. In fact, I am new to scripting/coding in general! I've done a few Codecademy courses on CSS and HTML and perhaps Java though this was all a few years back. I've recently come across AutoIt and decided to give it a try since I do quite a few repetitive tasks on a daily basis. In the last couple of weeks I've managed to master (or at least get comfortable with) mouse clicks(left/right), window focus, sending key strokes, controls, and pixel search.

Now let's get to the topic.

At this point in time I've tried out a few simple IE scripts but I am having difficulty understanding some things and tying everything together into one tool.

Specifically, I am struggling with this little bit of code I got from DaleHohm in his IE examples thread. Post #3 (The last example.)

#include <IE.au3>

$sImgDir = "c:\foo\"; Please make certain this folder already exists (silent failure if not)
$sWebPage = "http://www.autoitscript.com/forum/index.php?"; webpage with images

$oIE = _IECreate()
_IENavigate($oIE, $sWebPage)
$oIMGs = _IETagNameGetCollection($oIE.document, "img")

; Loop through all IMG tags and save file to local directory using INetGet
For $oIMG in $oIMGs
    $sImgUrl = $oIMG.src
    $sImgFileName = $oIMG.nameProp
    INetGet($sImgUrl,  $sImgDir & $sImgFileName)
Next

I have a couple questions about the code above.

1) ".src" ".nameProp" What are these called? I figured out that I can change the .src to something like .href and it gets anything on the webpage with a .href tag but where can I learn more about these? I still haven't been able to figure out what ".nameProp" is for or what it does. Is there any documentation/list of all the different ".PurpleTextAfterAVariable" (Edit: Not sure why it's red in the above example, just checked SciTE and it's purple there) that I can use?

2) I understand that the code above gets every "For $oIMG in $oIMGs" on the page but how can I make it only get the first 5? I've tried doing a "count" and a "for" but I am unsure what to replace the "For...in" statement with to keep the script functional. Is there a way to limit the _IETagNameGetCollection function to only get a specific amount of tags?

 

Finally, the reason I can't just use the code as is.

The site I am trying to get images from works in this way:

A href= "Link-To-Picture.jpg"

Img src= "Link-To-Picture-thumbnail.jpg"

The script above downloads every single thumbnail from the image gallery which is great, it does what it's supposed to but I need the full resolution image.

After changing the script to get anything with an "A href" tag it does what I need it to do, it gets every single image in full resolution... along with every single one of the 80-100 extra files/links to other sites that are listed under an "A href" tag.

 

Now I've come up with two solutions but unfortunately, as I mentioned above. I don't know how to put my solution into the code above to make it work.

Solution 1) Only get the first 5 instances of "A href" on the page.

As mentioned above. I don't know how to do this.

Solution 2) Read the entire page, find "-Thumbnail.jpg" replace with ".jpg" and use the script as is.

I understand how to do a replace. All I am missing is how to do a replace within a field in the code of an IE page. I assume that I have to use the HTMLRead functions but how do I use/alter the data read?

I really hope all of this make sense and that someone here will be able to help me figure out a solution to my issue or at least answer one of my questions! I do have plenty more questions and I am sure that I'll have even more by the time I figure this out.

Thank you very much for your time!

Edited by AnAdventurer
Double checked a color difference between SciTE and the forum code.

Share this post


Link to post
Share on other sites



Bump

Share this post


Link to post
Share on other sites

 

On 31/10/2016 at 6:59 AM, AnAdventurer said:

I figured out that I can change the .src to something like .href and it gets anything on the webpage with a .href tag

No. your code currently gets all the "img" tags and extracts the information from the ".src" attribute. Changing ".src" to ".href" would just give you the href attribute of the img tags. Based on your code you want the "a" tag, wrapped around your "img" tags? if so try my code below.

On 31/10/2016 at 6:59 AM, AnAdventurer said:

I still haven't been able to figure out what ".nameProp" is for or what it does. Is there any documentation/list of all the different ".PurpleTextAfterAVariable"

The "nameProp" property is acesseble only via the IE object you created in your code. IE documentation could point you in the right direction. Here is documentation for the "nameProp" property: nameProp property

 

The code below should be modified to work with your page.

#include <IE.au3>

$sImgDir = "c:\foo\"; Please make certain this folder already exists (silent failure if not)
$sWebPage = "http://www.autoitscript.com/forum/index.php?"; webpage with images

$oIE = _IECreate()
_IENavigate($oIE, $sWebPage)
$oIMGs = _IETagNameGetCollection($oIE.document, "img")

; Loop through all IMG tags and save file to local directory using INetGet
For $oIMG in $oIMGs
    $oA = $oIMG.parentNode
    If Not ($oA.localName == "a") Then ContinueLoop
    $sImgUrl = $oA.src
    $sImgFileName = $oA.nameProp
    INetGet($sImgUrl,  $sImgDir & $sImgFileName)
Next

 

Share this post


Link to post
Share on other sites
14 hours ago, genius257 said:

The code below should be modified to work with your page.

#include <IE.au3>

$sImgDir = "c:\foo\"; Please make certain this folder already exists (silent failure if not)
$sWebPage = "http://www.autoitscript.com/forum/index.php?"; webpage with images

$oIE = _IECreate()
_IENavigate($oIE, $sWebPage)
$oIMGs = _IETagNameGetCollection($oIE.document, "img")

; Loop through all IMG tags and save file to local directory using INetGet
For $oIMG in $oIMGs
    $oA = $oIMG.parentNode
    If Not ($oA.localName == "a") Then ContinueLoop
    $sImgUrl = $oA.src
    $sImgFileName = $oA.nameProp
    INetGet($sImgUrl,  $sImgDir & $sImgFileName)
Next

 

Thank you so much! I had to change some things around but it works now! I didn't even know you could use a parentnode! Is the msdn site you linked the best source for this type of thing?

Share this post


Link to post
Share on other sites
7 hours ago, AnAdventurer said:

Thank you so much! I had to change some things around but it works now!

Np :) Glad to hear it.

7 hours ago, AnAdventurer said:

Is the msdn site you linked the best source for this type of thing?

Yeah i would say so :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • PunkoHead
      IE function to click on nav button
      By PunkoHead
      Hi all,
      I am having an issue with a website that I want to automate.
      I have this menu and I want to click on the Custom button.

       
       
      The buttons with "inspect element" are: 
       

       
      The code that I tried is
      $sSearch = "Custom" $oFrame = _IEFrameGetObjByName($oIE, "options") $oDivs = _IETagNameGetCollection($oFrame, "ul") For $oDiv In $oDivs If String(_IEPropertyGet($oDiv, "InnerText")) = $sSearch Then _IEAction($oDiv, "click") ExitLoop EndIf Next I also tried with:
      $sMyString = "Custom" Local $oLinks = _IELinkGetCollection($oIE) For $oLink In $oLinks Local $sLinkText = _IEPropertyGet($oLink, "innerText") If StringInStr($sLinkText, $sMyString) Then _IEAction($oLink, "click") ExitLoop EndIf Next  
      However, these are not working. Any ideas?
       
       
    • rynow
      roma() - autoit Framework - (needed support)
      By rynow
      Hello,
      I´m using AutoIt for a long time not only to automate applications but to develop complex stand-alone applications. I am particularly annoyed by the fact that the logic in AutoIt is difficult to separate from the presentation and the standard GUI elements are very inflexible. If you want to create something more sophisticated, you have to use GDI and write many lines for simple effects or animations.
      With these thoughts in mind, I looked around for alternatives and unfortunately found nothing that corresponded to my ideas. Therefore, I have thought of a different solution. I have created this framework in order to separate the logic from the presentation and to use HTML & CSS in my GUI to the full extent.
      Goals of the Framework
      MVC development with AutoIt HTML & CSS GUI in AutoIt Better and more modern package system(UDF) like npm CLI support like Laravel Artisan faster and more structured application development roma() is strongly inspired by Laravel PHP Framework so Laravel users will notice many similarities.
      Support
      Unfortunately, I do not have much time for the project at the moment. So I thought to myself, I share it and ask you for support.
      Content
      The framework primarily serves for the development of stand-alone applications.
      All necessary settings are preconfigured. You can start immediately with the logic or the view All settings are in one place The logic(controller) and the presentation are clearly separated from each other Development with MVC structure You can develop the GUI in realTimer without restarting AutoIt GUI can be developed in HTML & CSS Any graphic & video integration is possible (.png, .gif etc.). Also everything that is possible in HTML5 and CSS3 JavaScript & Frameworks are supported Debug logs are created including console output It is possible to work with multiple languages All UDFs are contained in the framework. Reloading is not necessary The AutoIt UDFs are also included in the Framework. This ensures that it workds correctly for different Versions of AutoIt The framework also provides functions that are necessary for communication between AutoIt and HTML. For example, evaluation of form data (GET & POST) (documentation for this and examples follow.) I also developed a template engine. (Similar to Laravel Blade) The template engine supports if statements (would like to have help to make loops possible). In the Future I will publish a complete documentation of the template engine and examples. Almost finished is a database package. This makes communication with databases an absolute child's play. So that was it for once. If something else occurs to me, I will update the list. Small Example
      url: http://localhost:8080/welcome ;application.au3 ;----------------------------------------------------------------------------------------------/ ; Initial ;----------------------------------------------------------------------------------------------/ #include 'vendor\initial.au3' func _roma_routes() ;----------------------------------------------------------------------------------------------/ ; GET Request ;----------------------------------------------------------------------------------------------/ $route_get('welcome', 'welcome') endfunc ;roma\controller\welcome.au3 ;----------------------------------------------------------------------------------------------/ ; Welcome Controller ;----------------------------------------------------------------------------------------------/ func controller_welcome() Local $name = 'Eduard', $lastname = 'Tschernjaew' ;----------------------------------------------------------------------------------------------/ ; passed variable to view (Array are possible) ;----------------------------------------------------------------------------------------------/ $toView('name', $name) $toView('lastname', $lastname) ;----------------------------------------------------------------------------------------------/ ; Return a View ;----------------------------------------------------------------------------------------------/ return $VIEW('welcome') endfunc <html> <head> <title>roma() - Template Test</title> </head> <body> <h1>Hello {{ $name }} {{ $lastname }}</h1> </body> </html>  
      Download
      The framework is under the Open-Source license.
      Github: https://github.com/4ern/roma/
      git clone https://github.com/4ern/roma.git or download the ZIP. 
      Documentaion: https://github.com/4ern/roma/blob/master/README_EN.md 
      ToDo
      [ ] Loop Funktion in Template.au3
      [ ] CLI module like Laravel Artisan
      [ ] Solution approaches, how the framework can be optimally compiled, so that in the compiled state all files are available.
      [ ] Framework Tests & Bugfixes
      roma() is still in development. Documentation and application examples will soon be available. I am looking forward to any Contributing.
      Thanks for Feedback and Contributing
    • Dent
      How to download uniquely generated PDF?
      By Dent
      Hi everyone,
      My script uses IE11 on Win7 to log in to a site and enters data into a couple of forms. Upon clicking a link this data is used by the site to generate a PDF report.
      With my current set-up if I do this manually the PDF opens in a new IE tab and I can download or print it. If I right-click the link that creates the PDF and choose Save Target As the PDF is generated and the Open/Save As dialogue at the bottom of the screen opens. All good.
      However I would like the script to automatically download the PDF and close IE and then exit. Closing IE (_IEQuit) and exiting the script are easy enough, but I'm struggling getting the script to download the PDF.
      The link to generate the PDF contains a unique number each time the page with the link is reached, so it's not static. The link position however, using _IELinkGetCollection I can tell the link to generate the PDF is always the 10th one from the end of the page, so using $iNumLinks - 10 I am able to click the link.
      What I believe I need to use is InetGet however the problem I've been facing is that the link isn't static and I haven't worked out a way to access the link by index - is this possible?
      Here is the website HTML for the section containing the link although I don't think it's of much use but it at least shows the format of the link (I can't post a link as it's a password protected area)...
      <div class="rmButton right"><a title="Generates a PDF version of the market report in a new window." href="/rmplus/generatePdf?mr_id=60991" target="_blank">print/save as pdf</a></div> The full link https://www.rightmove.co.uk/rmplus/generatePdf?mr_id=60991 just for completeness - visiting it will give a HTTP 500 unless logged in.
      And here is the code that clicks this link opening the generated PDF in a new tab...
      $oLinks = _IELinkGetCollection($oIE) $iNumLinks = @extended $PrintPDF = _IELinkClickByIndex($oIE, ($iNumLinks - 10)) So, how to use InetGet to visit that link? Or is there a way to Save As the newly opened tab? I've tried _IEAction($oIE, "saveas") but it seems not to work in a tab containing only a PDF.
    • Juvigy
      IE automation
      By Juvigy
      Hi Guys,
      I have a very complex IE page with lots of Java and Iframes. I need to be able to look for a change in one <input> field. So what i have done is to identify the field and hook an event listener with ObjEvent($title, "_Evt_")  and then i use :
      Func _Evt_OnChange() Local $o_object = @COM_EventObj ConsoleWrite( "Change" & $o_object.value &@CRLF) _FileWriteLine($MainLog, "Change:" & _NOW()) TitleCheck($o_object.value) EndFunc Which work fine user doesn't interact with some of the other buttons or radios on the page. As soon as the user does some interaction or refreshes the page or navigates - the event listener is 'deleted' somehow and no longer works. Any idea how to counteract that? The easiest this is to detect if there is ANY change on the page - then i can identify the INPUT and check for its value. Any idea how to do that ?
    • SorryButImaNewbie
      [SOLVED: basic COM help] Working on string returned from API, m'i doing this okey?
      By SorryButImaNewbie
      Hello, I try to pull some data from a webpage.
      I need the value of local currency compared to the euro. I can go and open the required API page on the required date interval, read in from elsewhere, its format in the memory of the script is like this: 20161005 so YYYYMMDD.
      The return string if I try to view the opened API's source code is simple, but if I use _IEBodyReadHTML, _IEDocReadHTML, _IEBodyReadText i get it back with a lot of html code (i guess, it looks like HTML, and one of them doesn't show any string in the MsgBox when I try to chechk it) about its color etc. I need dates and the corresponding currency exchange rates (these can be found between  <kozep>exchangerate</kozep>, but I need the first only after every month because the second is the avarage exchange rate of the month (i guessed this again).
      Now I have an approach which will work evantually I guess, but I'm pretty sure its not the standard aproach or how the creators of autoit envisiond the useage of their functions
      So I post my code here hoping, someone tells me how to do this simply and inteligently.
      Sorry for such question but I only used regex for much, much simplier tasks. My approach is to identify everything I dont need basicly, after getting rid of a few key problematic chars (like " ) and do this untill I'm only left with what I need. THe problem with this if anything change in the envierment the script has like 99,9999% chance to not run properly, and I would like to handle this better, even if APIs usually don't change that much according to my knowladge.
      Also I write this in a separat function for now, I will plan to call it from my other function which does different things with the corresponding excel files, among them is the calculation of local currency values of the bills with data from MNB (Hungarian National Bank or something)
      Here is my code so far, and what its gives back, I will update this with the pic from the source code I see from internet explorer and the webpage I see. Thank you for your help and insight!
      Func InternetRead() ;Create the URL for napiarfolyam API #cs http://api.napiarfolyam.hu/?bank=mnb&valuta=eur&datum=20160901&datumend=20160926 </penznem> után jön a használt árfolyam Példa Return: <item> <bank>mnb</bank> <datum>2016-09-06 11:25:18</datum> <penznem>EUR</penznem> <kozep>309.8500</kozep> <kozep>310.1700</kozep> </item> #ce ;Global $MinTime ;20160601000000 these are example variables I read in, during the function that will call this one ;Global $MaxTime ;20160610000000 Local $URLbase = "http://api.napiarfolyam.hu/?bank=mnb&valuta=eur" ;view-source: ;Local $MinTimeFormated = StringTrimRight($MinTime, 6) ;Local $MaxTimeFormated = StringTrimRight($MaxTime, 6) Local $URL = $URLbase & ("&datum=" & "20160601" & "&datumend=" & "20160603" & "") ;20160603 $MinTimeFormated, $MaxTimeFormated MsgBox(64, "Értesítés", "URL:" & $URL & "") Local $oIE = _IECreate($URL) Sleep(1000) Local $sHTML = _IEDocReadHTML($oIE) ;_IEBodyReadHTML - Is string but, MsgBox shows nothing ;_IEDocReadHTML - at least retunrs something (extra then what i see from thw source code, ctrl+u) ;_IEBodyReadText - at least retunrs something (extra then what i see from thw source code, ctrl+u) $sHTML = String($sHTML) If IsString($sHTML) Then MsgBox(64, "HTML String?", "The variable is a string") Else MsgBox(64, "HTML String?", "The variable is not a string") EndIf ;Variable is a String! ;StringSplit ;">datum</span>&gt;</a>" & "20160601" Local $Stuff = Chr(34) ;The " char ;Local $Stuff2 = "<a xmlns=http://www.w3.org/1999/xhtml class=collapse style=color: blue; marginleft: 2em; position: relative; href=#>&lt;<span style=color: rgb(153,0,0);>" Local $StringInput = $sHTML Local $sHTML = StringRegExpReplace($StringInput, "[-]", "") Local $StringInput = $sHTML Local $sHTML = StringStripWS($StringInput, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES) Local $StringInput = $sHTML Local $sHTML = StringReplace($StringInput, $Stuff, "") ;Local $StringInput = $sHTML ;Local $sHTML = StringReplace($StringInput, $Stuff2, "") Local $StringInput = $sHTML Local $ValutaPosition = StringInStr($StringInput, "</valuta>") Local $sHTML = StringTrimLeft($StringInput, $ValutaPosition+8) Local $StringInput = $sHTML ;StringReplace($StringInput, "<a xmlns="http://" ;Local $StringInput = $sHTML ;StringInStr ;Local $sHTML = StringTrimLeft($StringInput, 1850) ;Local $aDays = StringSplit($sHTML, ">datum</span>&gt;</a>") ;_ArrayDisplay($aDays) ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "StringRegExpReplace Error", "Error listing:" & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox(64, "HTML String?", "$sHTML:" & $sHTML) EndFunc ;==>InternetRead  

      Edit:
      Sorry for the long post and I hope I was able to write dowm my problem in a way that others can understand, pls ask anything if you don't.