Advertisement
Guest User

Selenium Reference AutoHotKey

a guest
Feb 12th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Testing = 1 ;1 is Edit Mode,  2 is Testing
  2. if(Testing = 1)
  3. {
  4.     Edit
  5.     ExitApp
  6. }
  7. else, {}
  8.    
  9. ;------------------------------------ End of Ref AutoRun Section -----------------------------------
  10.  
  11. Global driver   ;Place in autorun section of script, Ensures that chrome is accessible when using functions within script
  12.  
  13.  
  14. ;-------------------------------- Defaults for Starting New Instance -------------------------------
  15. driver:= ComObjCreate("Selenium.CHROMEDriver") ;Select and Create Chrome driver instance
  16. driver.setbinary("C:\Software\Chromium\chrome.exe") ;Sets path to custom Binary
  17. driver.AddArgument("disable-infobars") ;Hides 'Chrome is being controlled by automated test software' message
  18. driver.AddArgument("--start-maximized") ; Maximize Chrome Browser
  19. driver.Get("LINKTOWEBSITE") ;Open selected URL
  20. driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
  21.  
  22.  
  23.  
  24. ;------------------------------------- Connect to Chrome Profile for Cookies ------------------------------------
  25.  
  26. driver:= ComObjCreate("Selenium.CHROMEDriver") ;Chrome driver
  27. ; put this "chrome://version/"  in the url in chrome to find path to profile
  28. ; https://stackoverflow.com/questions/25779027/load-default-chrome-profile-with-webdriverjs-selenium
  29. ; https://github.com/SeleniumHQ/selenium/issues/854
  30. driver.SetProfile("H:\Temp\Chrome\Cache\cache\Default") ; 'Full path of the profile directory
  31. driver.Get("LINKTOWEBSITE") ;Open selected URL
  32.  
  33.  
  34.  
  35. ;------------------------------------- Control the current Page ------------------------------------
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. driver.executeScript("INSERTCOPIEDJSHERE")  ;Executes a Javascript on the webpage, mostly used for buttons.
  43. ;OR
  44. JS = javascripttexthere (Make sure to remove the "Javascript:" part)
  45. driver.executeScript(JS)            ;Execute Javascript`
  46.  
  47. driver.executeScript("window.scrollTo(0, 0)")   ;Executes a Javascript to scroll the top of the webpage
  48. driver.executeScript("window.scrollTo(0, 2000)")    ;Executes a Javascript to scroll the webpage Down by 2000 pixels
  49.  
  50. driver.ExecuteScript("document.body.style.zoom = '100%';") ; Set the zoom of Chrome browser to 100%
  51. driver.ExecuteScript("document.body.style.zoom = '50%';") ; Set the zoom of Chrome browser to 50%
  52.  
  53.  
  54. ThisPage := driver.executeScript("return document.title") ;gets title of the Page and saves it as variable %ThisPage%
  55.  
  56. msgbox % driver.executeScript("return document.title") ;gets title of the Page and displays it as a msgbox
  57.  
  58. ;Switch focus to new window or tab.
  59. driver.ExecuteScript("window.open();")      ;Opens a new Window or tab (Depends on your Chrome Prefs)
  60. driver.SwitchToNextWindow               ;Focuses Selenium on the newly opened/next window.
  61. driver.close()  ;Closes current window or tab
  62. driver.SwitchToPreviousWindow               ;Focuses Selenium on the previously opened window.
  63.  
  64. ;---------------------------------- Selecting and Clicking Elements --------------------------------
  65. ;Click on Xpaths when XPATH has "quotation-marks on both ends".
  66. Xpath = INSERTXPATHHERE ;Saves Xpath as a variable.
  67. driver.FindElementByXPath(Xpath).click()    ;Clicks on Xpath based on variable.
  68.  
  69. driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.Enter)    ;Sends Enter key to Xpath.
  70.  
  71. ;Find Xpath and clicks it
  72. 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()
  73.  
  74. MsgBox % driver.FindElementById("content").Text ; Find element by ID and display result as msgbox
  75.  
  76. driver.findElementsByName("ELEMENTNAME").item[1].click()    ;;Clicks Item based on the Element Name
  77.  
  78. ;Clicks Item based on the Element Name and then sends to it the variable UN
  79. VAR = ExampleText       ;Sets the variable to be equal to "ExampleText"
  80. driver.findElementsByName("NAME").item[1].SendKeys(VAR)     ;selects element based on Name and sends variable to it.
  81. Msgbox % driver.findElementsByName("NAME").isSelected()   ;Display Msgbox with value of a selection box (Checkbox / Radio button)
  82. driver.findElementsByName("NAME").clear()   ;Clears content from an element and sets
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. driver.SendKeys(driver.Keys.ENTER)  ;Send Enter key to the page
  90.  
  91.  
  92. ;Clicks on Xpath based on the text of the object
  93. keyword:="Continue to Payment"  ;Finds the Text and clicks on it to get to the next page.
  94. if(driver.FindElementByXPath("//*[text() = '" keyword "']"))
  95.     driver.FindElementByXPath("//*[text() = '" keyword "']").click()
  96.  
  97.  
  98. ;Click on Xpaths when XPATH has "quotation-marks on both ends".
  99. Xpath = //*[@id="lst-ib"]
  100. driver.FindElementByXPath(Xpath).click()
  101. ;driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.Enter)
  102.  
  103.  
  104. ;Displays a msgbox with the contents of each of the following attributes that belong to the Xpath Item.
  105. Xpath = INSERTXPATHHERE
  106. MsgBox,,XPATH Inner Text,% driver.findelementbyxpath(Xpath).Attribute("innerText") ;GREAT FOR GRABBING INNER CONTENTS/Values
  107. MsgBox,,XPATH-ID & Tag,% driver.findelementbyxpath(Xpath).Attribute("outerHTML") ;XPath: ID=site-title & span tag
  108. MsgBox,,Xpath Value,% driver.findelementbyxpath(Xpath).Attribute("value") ;XPath: ID=site-title & span tag
  109. MsgBox,,XPATH Option Value,% driver.findelementbyxpath(Xpath).Attribute("option value") ;XPath: ID=site-title & span tag
  110.  
  111. MsgBox,,XPATH Text Content,% driver.findelementbyxpath(Xpath).Attribute("textContent") ;XPath: ID=site-title & span tag
  112.  
  113. MsgBox,,HREF Link Location: ,% driver.findelementbyxpath(Xpath).Attribute("href") ;XPath: Href (Link location) value
  114.  
  115.  
  116. ;send words and key button to browser window
  117. driver.findElementsByName("s").item[1].SendKeys("hello world").SendKeys(driver.Keys.ENTER)
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. ;------------------------------- Selenium Grab Current Chrome Instance -----------------------------
  125.  
  126. ;To have selenium grab onto current running chrome script, add the following to the RUN section of your chrome shortcut:
  127. chrome.exe --remote-debugging-port=9222
  128.  
  129. ;Grab the current instance of chrome instead of opening up a new chrome istance everytime:
  130. driver := ChromeGet()   ;Grabs the current open window of chrome
  131. MsgBox, % driver.Window.Title "`n" driver.Url   ;Displays the title of the current Chrome window ;Optional
  132. ;Function that grabs on to the current isntance of chrome.
  133. ChromeGet(IP_Port := "127.0.0.1:9222") {
  134.     driver := ComObjCreate("Selenium.ChromeDriver")
  135.     driver.SetCapability("debuggerAddress", IP_Port)
  136.     driver.Start()
  137.     return driver
  138. }
  139. F12::
  140. driver.Start()
  141. MsgBox, % driver.Window.Title "`n" driver.Url
  142. return
  143.  
  144. ;------------------------------- Exit Script when Chrome gets closed. ------------------------------
  145.  
  146. Loop {  ;Checks if Exel is still open, and if not,  kills the script.
  147.     if !WinExist("ahk_class Chrome_WidgetWin_1")
  148.         ExitApp
  149.     else
  150.         Sleep, 300000   ;Sleep for 5 minutes.
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement