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

    • nassausky
      By nassausky
      Hi all,
       
      Anyone have any idea how to close all open tabs except a specific one I manually open.  Assuming I don't know what is open in all the tabs except just the one I want to keep open.
       
      I didn't want to use sendkeys and I was trying to use the following code to list the title (or url) of the 3 open tabs and  after I got that part working I would just close the other 2. This sample only displays the title of the first open tab
      #include <IE.au3> Const $ie_new_in_tab = 0x0800 $oIE = _IECreate("https://www.autoitscript.com") __IENavigate($oIE, "https://www.autoitscript.com/forum/", 1, $ie_new_in_tab) ;(obj,url,wait,param) __IENavigate($oIE, "https://www.google.com/", 1, $ie_new_in_tab) ;(obj,url,wait,param) Local $aIE[1] $aIE[0] = 0 Local $i = 1, $oIE While 1     $oIE = _IEAttach("", "instance", $i)     If @error = $_IEStatus_NoMatch Then ExitLoop     ConsoleWrite(_IEPropertyGet($oIE, "title") & @CRLF)     ReDim $aIE[$i + 1]     $aIE[$i] = $oIE ;each item holds object     $aIE[0] = $i ;first item holds count     $i += 1 WEnd MsgBox($MB_SYSTEMMODAL, "Browsers Found", "Number of browser instances in the array: " & $aIE[0]) ; This doesn't return the list of tabs in the console just the first tab  
      Thanks for any and all help
    • toto22
      By toto22
      I'm trying to click on Java Dropbox using IE. However, I'm running into problems. There is a Dropbox "Please Select" with two options "Buy" and "Sell".
      I'm able to click on a drop box (please see code below) but i'm unable to select "Buy" or "Sell"".
      Local $sMyString = "Please Select" ;############ ENTER ############# Local $oLinks = _IELinkGetCollection($oIE) For $oLink In $oLinks Local $sLinkText = _IEPropertyGet($oLink, "innerText") If StringInStr($sLinkText, $sMyString) Then _IEAction($oLink, "click") ExitLoop EndIf Next  
      Please help
       
         
    • Gowrisankar
      By Gowrisankar
      Hello everyone,
      I'm working on a task where, a PDF file is opened (in IE browser) when I click a link in a website.
      I have to read the first page of the PDF to find particular strings. Can you please share some ideas?
    • Seminko
      By Seminko
      Hey,
      i would like to set a value into an INPUT field.
      Checked the _IEFormElementSetValue function but that does require _IEFormGetObjByName and this is where the problem comes in. The input field I want to write to is not a part of a form tag. It is part of a table.
      <input type="text" class="w2" id="nabidka_vozidel_formular_tach_od" name="nabidka_vozidel_formular_tach_od" onchange="GLOBAL.pocetInzerceNZ(&quot;nabidka_vozidel_formular&quot;,&quot;tach_od&quot;,&quot;&quot;);" autocomplete="off"> I tried this but that didn't work:
      $oDownloadSamples = _IEGetObjById($oIE, "nabidka_vozidel_formular_tach_od") _IEFormElementSetValue($oDownloadSamples, "123") If you want to try the site I'm working with is https://www.tipcars.cz/. There is a menu on the top left hand side and if you click the "vyhledat" button the input fields will show up.
      Thanks
    • Seminko
      By Seminko
      I am now able to populate the dropdown menus at https://www.tipcars.com/.
      The dropdowns are dynamic and do not have a Text value, only ids.
      Is there a way to get all the available options from a dropdown?
      $url = "https://www.tipcars.com/" $oIE = _IECreate($url) _IELoadWait($oIE) $oDownloadSamples = _IEGetObjById($oIE, "homepage_vyhl_frm_vozidlo") _IEFormElementOptionSelect($oDownloadSamples, "C") Above code, populates ID 'C' into the 'druh' dropdown, which results in selecting 'užitkové'. 
      Is there a way so I can grab all of the options from the dropdown menu? I could easily grab all the IDs but I am afraid they will change in the future so I would much rather match the text titles and assign the option IDs grabbed from site.
      This is what the options are according to Chrome:
      <select class="w1"id="homepage_vyhl_frm_vozidlo"name="homepage_vyhl_frm_vozidlo"onchange="try{NABIDKA_VOZIDEL_FORMULAR.naplnSelectZnacka('homepage_vyhl_frm');GLOBAL.pocetInzerceHZ('homepage_vyhl_frm','vozidlo');}catch(ex){odeslatJsChybu('formHOMEPAGE onChange 1: ' + ex.message, 'formHomePage onChange 1', -1);}"> <option value="A" style="color: black;">osobní a terénní (56 334)</option> <option value="C" style="color: black;">užitkové (4 333)</option> <option value="D" style="color: black;">nákladní (3 742)</option> <option value="E" style="color: black;">autobusy (56)</option> <option value="F" style="color: black;">obytné (373)</option> <option value="G" style="color: black;">přívěsy (1 179)</option> <option value="J" style="color: black;">motorky (780)</option> <option value="S" style="color: black;">pracovní stroje (605)</option> <option value="L" style="color: black;">ostatní (235)</option></select> Also, this cannot be grabbed by _INetGetSource.
      Thanks