ur

Get IE element by CSSSelector

9 posts in this topic

#1 ·  Posted

We can select elements in IE using their IDs as below.

Local $oDiv = _IEGetObjById($oIE, "x-auto-16-input")

But to one button in the webpage, there is no ID to it.

In selenium we have option to select this element using the CssSelector and clicked the button using below code in c# selenium.

driver.FindElement(By.CssSelector("button.x-btn-text")).Click();

What is the alternative for this in AUtoIT?

Share this post


Link to post
Share on other sites



#2 ·  Posted

THe inner html

<button class="x-btn-text" type="button" style="position: relative; width: 64px;" tabindex="0">Initialize</button>

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

Have you tried searching the forums? I believe there are numerous examples of clicking a button by class.

Share this post


Link to post
Share on other sites

#4 ·  Posted

I tried but getting only the results for based on tag name but not for classselector.

If possible can you share any

Share this post


Link to post
Share on other sites

#5 ·  Posted

How about this thread?

 

Share this post


Link to post
Share on other sites

#6 ·  Posted

Thanks @Danp2

I am able to click the button using a loop for reading the classname of all the buttons in the pages.

Even though it is not selecting the button directly as in Selenium, the current solution is satisfactory.

Func clickButtonWithClass($oIE,$sClass)
      $oFound = ""
      Local $oBtns = _IETagNameGetCollection($oIE, "button")
      For $oBtn In $oBtns
          If String($oBtn.classname) = $sClass Then
           $oFound = $oBtn
         EndIf
      Next
      If IsObj($oFound) Then
         _IEAction ($oFound, "click")
         _IELoadWait ($oIE)
         MsgBox(16, "Success", "Button found with class name: "& $sClass &" and clicked",10)
      Else
         MsgBox(16,"Not found." , "Button not found with class name: "& $sClass,10)
      EndIf
EndFunc

But I am not able to get the page results when I tried from search toolbar on the top of page in this forum.

And also, is there anyway  to use XPath in AutoIT.?

I tried to search some pages but they are looping inside each element tag from body tag onwards, which is not working with below XPath.

driver.FindElement(By.XPath("(//button[@type='button'])[5]")).Click();

Any suggestions?

Share this post


Link to post
Share on other sites

#7 ·  Posted

Glad you found something that works. FWIW, You should be able to add an ExitLoop to break out of the For...Next loop once the button is found.

Here's one option for searching with XPath --

 

Share this post


Link to post
Share on other sites

#8 ·  Posted

Another method would be to use jQuery to select the object, like this --

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Another solution arises once you realize $oIE is an internetexplorer.application object. What does this mean? You can access the page as an object with $oIE.document. This lets you do most things javascript can do with almost the same syntax!

for instance:

; Get all elements in the page by class name
$oElement = $oIE.getElementsByClassName("whatever comes after the = in class=''")
; This returns an array object, so we set $oElement to that object specificly
$oElement = $oTemp(0)
; We can then set it's value or text directly
$oElement.value = 45 ; used mostly for inputs, your milage might vary
$oElement.text = some text ; The text that's between the > and < that people see (usually)
; We can make events, and send them to $oElement to trigger javascript

$oClickEvent = $oIE.document.createEvent('MouseEvents')
;Event types are 'mouseover', 'mousedown', 'mouseup', and 'click'
$oClickEvent.initEvent($action, true, true)
;Now that we have the event, we send it to the control we want
$node.dispatchEvent ($oClickEvent)

;or even other events

$oEvent = $oIE.document.createEvent('Events')
$oEvent.initEvent('submit', true, true)
$node.dispatchEvent ($oEvent)

Once you realize $oIE.document (or any element you get with the other _IE functions are all objects too!) you can start googling java script examples, and by just changing the variables from document or this to $oIE.document or $oElement you can get many of them to work!
 

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

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

    • BlazerV60
      By BlazerV60
      Hello all,
      I've written the code below which launches chrome in incognito mode and then proceeds to go to the autoit website.
      From my understanding, the Run() command is also supposed to output the PID number related to the application that got launched from the Run command.
      However when I run the below lines, it outputs a PID number that is different from the newly launched chrome browser's PID number, does anyone know why and possibly explain how I could retrieve the accurate PID number associated with the newly launched browser?
      Global $iPid = Run(@ComSpec & ' /c start chrome.exe https://www.autoitscript.com/forum/ -incognito' ,"", "") msgbox(0,"",$iPid) Thank you,
      Brian
    • BlazerV60
      By BlazerV60
      Hello all,
      I've been trying to figure out how to launch Google Chrome in the background (hidden) but it doesn't seem possible. I've tried the following methods:
      ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "", "", "", @SW_HIDE) Also I've tried:
      ShellExecute(@ComSpec, "/c start chrome.exe","","",@SW_HIDE) Lastly I tried:
      RunWait('"'&@ProgramFilesDir&'\Google\Chrome\Application\chrome.exe" --silent-launch',@ScriptDir)  
      But all of them launch my chrome browser without hiding it. Does anyone know a workaround for this or if AutoIT just can't Chrome? 
      Bonus points if you know how to make it launch chrome hidden and make it go to https://www.autoitscript.com
       
      Thank you,
      Brian
    • Spask
      By Spask
      Hi, I'm trying to find a text value inside of a html.
      This is what the line looks like normally:
      <p id="line1" class> <span class="bot">TEXT HERE</span> </p> The text then changes to a non breaking space:
      <p id="line1" class> <span class="bot">&nbsp;</span> </p> And then it changes back to normal text but it's different every time.
      Can I code this so that it grabs the text every time it changes and has a variable that represents it?
      I currently have this inside of my loop:
      $span = .document.getElementsByTagName("span") For $text In $span If $text.value = "&nbsp;" Then Sleep(50) MsgBox(0,0,0) ;messagebox to test if it can be found, but I don't know how to grab the text EndIf Next The problem is that there are many other lines in the html that have the same span but are called "line3", "line5", etc and the one I need is from "line1".
      I will appreciate if anyone can help with this!
    • BOUNCER
      By BOUNCER
      I'm trying to get the output data from https://www.guilded.gg but it uses a web app that generates that output on the fly and this doesn't show up in the page source.
      Only in web inspector of each browser does this data show up. How do I read this data in autoit (equivalent to the web inspector)
       
      thank you
    • electrons
      By electrons
      hi  , hope  you all doing well 
       cant find any elements on the page ?  and the right click mouse dont work too?
      iam trying with more than method , i need help please!!