Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Testing = 1 ;1 is Edit Mode, 2 is Testing
- if(Testing = 1)
- {
- Edit
- ExitApp
- }
- else, {}
- ;------------------------------------ End of Ref AutoRun Section -----------------------------------
- Global driver ;Place in autorun section of script, Ensures that chrome is accessible when using functions within script
- ;-------------------------------- Defaults for Starting New Instance -------------------------------
- driver:= ComObjCreate("Selenium.CHROMEDriver") ;Select and Create Chrome driver instance
- driver.setbinary("C:\Software\Chromium\chrome.exe") ;Sets path to custom Binary
- driver.AddArgument("disable-infobars") ;Hides 'Chrome is being controlled by automated test software' message
- driver.AddArgument("--start-maximized") ; Maximize Chrome Browser
- driver.Get("LINKTOWEBSITE") ;Open selected URL
- driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
- ;------------------------------------- Connect to Chrome Profile for Cookies ------------------------------------
- driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
- ; put this "chrome://version/" in the url in chrome to find path to profile
- ; https://stackoverflow.com/questions/25779027/load-default-chrome-profile-with-webdriverjs-selenium
- ; https://github.com/SeleniumHQ/selenium/issues/854
- driver.SetProfile("H:\Temp\Chrome\Cache\cache\Default") ; 'Full path of the profile directory
- driver.Get("LINKTOWEBSITE") ;Open selected URL
- ;------------------------------------- Control the current Page ------------------------------------
- driver.executeScript("INSERTCOPIEDJSHERE") ;Executes a Javascript on the webpage, mostly used for buttons.
- ;OR
- JS = javascripttexthere (Make sure to remove the "Javascript:" part)
- driver.executeScript(JS) ;Execute Javascript`
- driver.executeScript("window.scrollTo(0, 0)") ;Executes a Javascript to scroll the top of the webpage
- driver.executeScript("window.scrollTo(0, 2000)") ;Executes a Javascript to scroll the webpage Down by 2000 pixels
- driver.ExecuteScript("document.body.style.zoom = '100%';") ; Set the zoom of Chrome browser to 100%
- driver.ExecuteScript("document.body.style.zoom = '50%';") ; Set the zoom of Chrome browser to 50%
- ThisPage := driver.executeScript("return document.title") ;gets title of the Page and saves it as variable %ThisPage%
- msgbox % driver.executeScript("return document.title") ;gets title of the Page and displays it as a msgbox
- ;Switch focus to new window or tab.
- driver.ExecuteScript("window.open();") ;Opens a new Window or tab (Depends on your Chrome Prefs)
- driver.SwitchToNextWindow ;Focuses Selenium on the newly opened/next window.
- driver.close() ;Closes current window or tab
- driver.SwitchToPreviousWindow ;Focuses Selenium on the previously opened window.
- ;---------------------------------- Selecting and Clicking Elements --------------------------------
- ;Click on Xpaths when XPATH has "quotation-marks on both ends".
- Xpath = INSERTXPATHHERE ;Saves Xpath as a variable.
- driver.FindElementByXPath(Xpath).click() ;Clicks on Xpath based on variable.
- driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.Enter) ;Sends Enter key to Xpath.
- ;Find Xpath and clicks it
- driver.FindElementByXPath("/html/body/fg-root/div[1]/fg-public-layout/fg-auth/div[1]/div/div/div[1]/div/div/form/div[1]/input").click()
- MsgBox % driver.FindElementById("content").Text ; Find element by ID and display result as msgbox
- driver.findElementsByName("ELEMENTNAME").item[1].click() ;;Clicks Item based on the Element Name
- ;Clicks Item based on the Element Name and then sends to it the variable UN
- VAR = ExampleText ;Sets the variable to be equal to "ExampleText"
- driver.findElementsByName("NAME").item[1].SendKeys(VAR) ;selects element based on Name and sends variable to it.
- Msgbox % driver.findElementsByName("NAME").isSelected() ;Display Msgbox with value of a selection box (Checkbox / Radio button)
- driver.findElementsByName("NAME").clear() ;Clears content from an element and sets
- driver.SendKeys(driver.Keys.ENTER) ;Send Enter key to the page
- ;Clicks on Xpath based on the text of the object
- keyword:="Continue to Payment" ;Finds the Text and clicks on it to get to the next page.
- if(driver.FindElementByXPath("//*[text() = '" keyword "']"))
- driver.FindElementByXPath("//*[text() = '" keyword "']").click()
- ;Click on Xpaths when XPATH has "quotation-marks on both ends".
- Xpath = //*[@id="lst-ib"]
- driver.FindElementByXPath(Xpath).click()
- ;driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.Enter)
- ;Displays a msgbox with the contents of each of the following attributes that belong to the Xpath Item.
- Xpath = INSERTXPATHHERE
- MsgBox,,XPATH Inner Text,% driver.findelementbyxpath(Xpath).Attribute("innerText") ;GREAT FOR GRABBING INNER CONTENTS/Values
- MsgBox,,XPATH-ID & Tag,% driver.findelementbyxpath(Xpath).Attribute("outerHTML") ;XPath: ID=site-title & span tag
- MsgBox,,Xpath Value,% driver.findelementbyxpath(Xpath).Attribute("value") ;XPath: ID=site-title & span tag
- MsgBox,,XPATH Option Value,% driver.findelementbyxpath(Xpath).Attribute("option value") ;XPath: ID=site-title & span tag
- MsgBox,,XPATH Text Content,% driver.findelementbyxpath(Xpath).Attribute("textContent") ;XPath: ID=site-title & span tag
- MsgBox,,HREF Link Location: ,% driver.findelementbyxpath(Xpath).Attribute("href") ;XPath: Href (Link location) value
- ;send words and key button to browser window
- driver.findElementsByName("s").item[1].SendKeys("hello world").SendKeys(driver.Keys.ENTER)
- ;------------------------------- Selenium Grab Current Chrome Instance -----------------------------
- ;To have selenium grab onto current running chrome script, add the following to the RUN section of your chrome shortcut:
- chrome.exe --remote-debugging-port=9222
- ;Grab the current instance of chrome instead of opening up a new chrome istance everytime:
- driver := ChromeGet() ;Grabs the current open window of chrome
- MsgBox, % driver.Window.Title "`n" driver.Url ;Displays the title of the current Chrome window ;Optional
- ;Function that grabs on to the current isntance of chrome.
- ChromeGet(IP_Port := "127.0.0.1:9222") {
- driver := ComObjCreate("Selenium.ChromeDriver")
- driver.SetCapability("debuggerAddress", IP_Port)
- driver.Start()
- return driver
- }
- F12::
- driver.Start()
- MsgBox, % driver.Window.Title "`n" driver.Url
- return
- ;------------------------------- Exit Script when Chrome gets closed. ------------------------------
- Loop { ;Checks if Exel is still open, and if not, kills the script.
- if !WinExist("ahk_class Chrome_WidgetWin_1")
- ExitApp
- else
- Sleep, 300000 ;Sleep for 5 minutes.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement