js path good in dev tools bad chrome.ahk Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

js path good in dev tools bad chrome.ahk

Post by HeXaDeCiMaToR » 31 Mar 2021, 03:52

I wrote a script similar to this for another site. It works great on the other site but on the current site when I hit "RIP" (scrape) it returns empty values (MsgBox and in the Gui, Edit). I wrote in an alert to make sure I was connected to chrome and the alert works fine, and a button click that does nothing in the Paste Button of the Gui. All of the JS Paths work in Dev Tools, but I Plug them into the script and run it... as I said before every method of looking at the variable values comes up blank. The odd thing is that I'm not even getting any "null" value errors.

I've been banging my head on this for HOURS and I just can't figure out what I'm doing wrong.

In the script is the URL of one of the pages I was scraping.

Any information would be helpful. Even if you know a better way to script what I've written.

Thanks

PS: I checked my other script and it still fine... sigh...

Code: Select all

#SingleInstance, Force
#NoEnv
#Include C:\Users\tdidr\Desktop\Scripts\Chrome.ahk_v1.2\Chrome.ahk
SetBatchLines, -1
SendMode Input
SetTitleMatchMode 2
SetWorkingDir,%A_ScriptDir%

;~ Current Web Page: https www.truckpaper.com /listings/trucks/for-sale/202870935/2015-freightliner-cascadia-125-evolution  Broken Link for safety

Gui, +AlwaysOnTop

Gui, Add, Edit, x280 y10 w50   h20 vEF_Price, Price
Gui, Add, Edit, x20 y40 w310   h20 vEF_Dealer_Name, Dealer Name
Gui, Add, Edit, x20 y+10 w150  h20 vEF_Postal_Code, Postal Code
Gui, Add, Edit, x20 y+10 w150  h20 vEF_Phone, Phone
Gui, Add, Edit, x20 y+10 w150  h20 vEF_Stock_Number, Stock Number
Gui, Add, Edit, x20 y+10 w150  h20 vEF_Mileage, Mileage
Gui, Add, Edit, x20 y+10 w150  h20 vEF_VIN , VIN
Gui, Add, Edit, x20 y+10 w150  h20 vEF_Horsepower, Horsepower
Gui, Add, Edit, x20 y+10 w150  h20 vEF_Engine_Displacement, Engine Displacement

Gui, Add, Edit, x180 y70 w150  h20 vEF_Transmission, Transmission
Gui, Add, Edit, x180 y+10 w150 h20 vEF_Transmission_Type, Transmission Type
Gui, Add, Edit, x180 y+10 w150 h20 vEF_Number_Of_Speeds, Number Of Speeds
Gui, Add, Edit, x180 y+10 w150 h20 vEF_Suspension, Suspension
Gui, Add, Edit, x180 y+10 w150 h20 vEF_Wheels, Wheels
Gui, Add, Edit, x180 y+10 w150 h20 vEF_Sleeper, Sleeper
Gui, Add, Edit, x180 y+10 w150 h20 vEF_Sleeper_Size, Sleeper Size

Gui, Add, Edit, x20 y+10 w310  h100 vEF_Discription, Discription
Gui, Add, Edit, x20 y+10 w310  h100 vEF_Web_Address, Web Address

Gui, Add, Button, x20 y500 w100 h30 gRIP, RIP
Gui, Add, Button, x230 y500 w100 h30 gPaste, PASTE
SW := A_ScreenWidth-450
SH := A_ScreenHeight-650
;~ Gui, Show, w350 h550 NoActivate, TruckPaper RIP
Gui, Show, x%sw% y%sh% w350 h550, TruckPaper RIP
return

Paste:
page := Chrome.GetPage()
page.Evaluate("alert('HI!')")
BT_Click = document.querySelector("#ListListing_ > div.list-details.cf > div.list-contact > a.list-btn.collapsible-contacts")
page.Evaluate(BT_Click).click()
return

RIP:
page := Chrome.GetPage()
page.Evaluate("alert('HI!')")

;~ WEB ADDRESS
Web_Address = window.location.href
Web_Address := page.Evalute(Web_Address).value

MsgBox URL: %Web_Address%

;~ DEALER NAME
Dealer_Owner = document.getElementsByClassName("fixed-column6 left print-full")[0].children[0].innerText
;~ Dealer_Owner = document.getElementsByClassName("fixed-column6 left print-full")[0].children[0].value
;~ Dealer_Owner = document.getElementsByClassName('fixed-column6 left print-full')[0].children[0].innerText
;~ Dealer_Owner := page.Evalute(Dealer_Owner).innerText
Dealer_Owner := page.Evalute(Dealer_Owner).value

MsgBox Dealership: %Dealer_Owner% - %Dealer_Owner2%

;~ DEALER ZIP
Dealer_Zip = document.getElementsByClassName("machinelocation")[0].innerText ; Needs RegEx \d{5}
Dealer_Zip := page.Evalute(Dealer_Zip).value
RegExMatch(Dealer_Zip, "\d{5}", Dealer_Zip)

;~ MsgBox Zip: %Dealer_Zip%

;~ DEALER PHONE
Dealer_Phone = $("a[href*='tel:']")[1].innerText
Dealer_Phone := page.Evalute(Dealer_Phone).value

;~ STOCK NUMBER
Stock_Number = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Stock Number"))[0].nextElementSibling.innerText
Stock_Number := page.Evalute(Stock_Number).value

;~ ASSET PRICE
Asset_Price = document.querySelector("span.price-value").innerText
Asset_Price := page.Evalute(Asset_Price).value

;~ MILEAGE/ODOMETER
Mileage_Odometer = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Mileage"))[0].nextElementSibling.innerText
Mileage_Odometer := page.Evalute(Mileage_Odometer).value

;~ VIN CODE
VIN_Code = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("VIN"))[0].nextElementSibling.innerText
VIN_Code := page.Evalute(VIN_Code).value

;~ HORSEPOWER
Horse_Power = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Horsepower"))[0].nextElementSibling.innerText
Horse_Power := page.Evalute(Horse_Power).value

;~ ENGINE DISPLACEMENT
Engine_Displacement = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Engine Displacement"))[0].nextElementSibling.innerText
Engine_Displacement := page.Evalute(Engine_Displacement).value

;~ TRANSMISSION
Trans_mission = [].filter.call(document.querySelectorAll('.spec-name'), (e) => e.innerText.startsWith('Transmission'))[0].nextElementSibling.innerText
Trans_mission := page.Evalute(Trans_mission).value

;~ TRANSMISSION TYPE
Transmission_Type = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Transmission Type"))[0].nextElementSibling.innerText
Transmission_Type := page.Evalute(Transmission_Type).value

;~ TRANSMISSION NUMBER OF SPEEDS
Num_Of_Speeds = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Number of Speeds"))[0].nextElementSibling.innerText
Num_Of_Speeds := page.Evalute(Num_Of_Speeds).value

;~ SUSPENSION
Sus_pension = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Suspension"))[0].nextElementSibling.innerText
Sus_pension := page.Evalute(Sus_pension).value

;~ WHEELS
Wheels_Rims = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Wheels"))[0].nextElementSibling.innerText
Wheels_Rims := page.Evalute(Wheels_Rims).value

;~ SLEEPER HEIGHT
Sleeper_Height = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Sleeper"))[0].nextElementSibling.innerText
Sleeper_Height := page.Evalute(Sleeper_Height).value

;~ SLEEPER DEPTH
Sleeper_Depth = [].filter.call(document.querySelectorAll(".spec-name"), (e) => e.innerText.startsWith("Sleeper Size"))[0].nextElementSibling.innerText
Sleeper_Depth := page.Evalute(Sleeper_Depth).value



;~ DISCRIPTION
Di_scription = document.querySelector("h3.theme-text-color").nextElementSibling.innerText
Di_scription := page.Evalute(Di_scription).value
 
 MsgBox Dealer:%Dealer_Owner%`nZip:%Dealer_Zip%`nPhone:%Dealer_Phone%`nStock:%Stock_Number%`nMileage:%Mileage_Odometer%`nVIN:%VIN_Code%`nHP:%Horse_Power%`nDisplacement:%Engine_Displacement%`nTransmission:%Trans_mission%`nTrans Type:%Transmission_Type%`nNum Of Speeds:%Num_Of_Speeds%`nSuspension:%Sus_pension%`nWheels:%Wheels_Rims%`nSleeper:%Sleeper_Height%`nBunk:%Sleeper_Depth%`nDiscription:%Di_scription%`nURL:%Web_Address%

Gui, Submit, NoHide
GuiControl, Text, EF_Dealer_Name, %Dealer_Owner%
GuiControl, Text, EF_Postal_Code, %Dealer_Zip%
GuiControl, Text, EF_Phone, %Dealer_Phone%
GuiControl, Text, EF_Stock_Number, %Stock_Number%
GuiControl, Text, EF_Mileage, %Mileage_Odometer%
;~ GuiControl, Text, Asset_Price, %Asset_Price%
GuiControl, Text, EF_VIN, %VIN_Code%
GuiControl, Text, EF_Horsepower, %Horse_Power%
GuiControl, Text, EF_Engine_Displacement, %Engine_Displacement%
GuiControl, Text, EF_Transmission, %Trans_mission%
GuiControl, Text, EF_Transmission_Type, %Transmission_Type%
GuiControl, Text, EF_Number_Of_Speeds, %Num_Of_Speeds%
GuiControl, Text, EF_Suspension , %Sus_pension%
GuiControl, Text, EF_Wheels, %Wheels_Rims%
GuiControl, Text, EF_Sleeper, %Sleeper_Height%
GuiControl, Text, EF_Sleeper_Size, %Sleeper_Depth%
GuiControl, Text, EF_Discription, %Di_scription%
GuiControl, Text, EF_Web_Address, %Web_Address%
page.Disconnect()
return

GuiClose:
page.Disconnect()
ExitApp

HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: js path good in dev tools bad chrome.ahk  Topic is solved

Post by HeXaDeCiMaToR » 31 Mar 2021, 10:54

:::::::::::::::::SOLVED:::::::::::::::::

In "page.Evaluate", "Evaluate" was missing an "A" after the "U".

I need to write a hotstring for that...

maleko48
Posts: 15
Joined: 19 Jul 2019, 13:35

Re: js path good in dev tools bad chrome.ahk

Post by maleko48 » 14 Apr 2021, 08:35

Thanks for this thread. Your code helped me realize what was wrong with my call to get the current page URL.

I was trying (unsuccessfully) to do:

Code: Select all

pageURL := PageInst.Evaluate("window.location.href")
MsgBox, pageURL: %pageURL%

What worked for me instead was:

Code: Select all

url = window.location.href
pageURL := PageInst.Evaluate(url).value
MsgBox, pageURL: %pageURL%
I suppose it has something to do with the different types of quotes and escaping the periods correctly if I really wanted to jam it all into 1 line / command, but that is not critical for my application.

Cheers!

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: js path good in dev tools bad chrome.ahk

Post by boiler » 14 Apr 2021, 13:11

maleko48 wrote: I suppose it has something to do with the different types of quotes and escaping the periods correctly if I really wanted to jam it all into 1 line / command, but that is not critical for my application.
The difference is that you have .value on the one that works but not the one that doesn’t.

gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: js path good in dev tools bad chrome.ahk

Post by gregster » 14 Apr 2021, 13:20

boiler is right. The chrome debugging protocol/Chrome.ahk returns an object (via its Evaluate() method) with different properties.
Primitive string values, like an URL, are usually contained in its value property (this is not the same like javascript's value property).

maleko48
Posts: 15
Joined: 19 Jul 2019, 13:35

Re: js path good in dev tools bad chrome.ahk

Post by maleko48 » 14 Apr 2021, 16:54

Thanks guys for explaining the details. I had tried to attach a ".value " or ".value()" to my first attempt, but obviously I was doing it wrong. I must have been trying to grab the javascript value instead of the Chrome.ahk's Evaluate's value.

This is working as expected for me now:

Code: Select all

pageURL := PageInst.Evaluate("window.location.href").value

HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: js path good in dev tools bad chrome.ahk

Post by HeXaDeCiMaToR » 16 Apr 2021, 09:48

maleko48, ahk gets really glitchy with JS Paths that aren't assigned to a variable ("URL" in this case). So the two differences with your first and second examples are .value like Boiler pointed out, and the fact that you're plugging the JS Path directly into Evaluate(). Someone WAY smarter than I am will probably come in and explain it lol.

But this is why I assign ALL of my JS Paths to a variable. Not only that but what if I have to use it again? BOOM! plug that var back into the position I need it and keep on truck'n.

Btw... I know it's only been 2 weeks since the op, but my script looks COMPLETELY different! I was reading through what I originally posted and I was like "Why would I write that like that!?!?" and "Nope! That should've gone over there!".

Thank you for all of your past, present, and future help!

Post Reply

Return to “Ask for Help (v1)”