Jump to content

Click IE object by text


Recommended Posts

Hey there! :)

I am having a problem with clicking a button in IE. As you can see in the code, there would be a class name to click it, but since this button exists several times with exactly the same code, I can only use the "Noch nicht veröffentlicht" to separate it from the others. But this part is just pure text without any classification. 

"<div class="checkmark" data-v-1e5b1f70=""><svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 16 16" data-v-1e5b1f70=""><circle cx="8" cy="8" r="8" data-v-1e5b1f70="" /></svg></div>Noch nicht veröffentlicht"

I was trying to click it with:

#include <IE.au3>
   local $oNNVs = _IEFormGetCollection($oIE2)
   For $oNNV in $oNNVs
       If String($oNNV.innertext) = "Noch nicht veröffentlicht" Then
           _IEAction($oNNV, "click")
       EndIf
    Next

and:

$inputs = $oIE2.document.getElementsByTagName("div")
For $input In $inputs
    If $input.innertext == "Noch nicht veröffentlicht" then
        $input.click()
    EndIf
Next

But nothing seems to work. Thanks for the help!

Link to comment
Share on other sites

Try something like:
nb: Untested

#include <IE.au3>

Global $oIE = _IECreate("url")
If IsObj($oIE) Then
    $oDivs = _IETagNameGetCollection($oIE, "div")
    If IsObj($oDivs) Then
        For $oDiv In $oDivs
            If IsObj($oDiv) Then
                If $oDiv.className = "checkmark" And $oDiv.innerText = "Noch nicht veröffentlicht" Then
                    _IEAction($oDiv, "click")
                EndIf
            EndIf
        Next
    EndIf
EndIf

 

Link to comment
Share on other sites

47 minutes ago, Subz said:

Try something like:
nb: Untested

#include <IE.au3>

Global $oIE = _IECreate("url")
If IsObj($oIE) Then
    $oDivs = _IETagNameGetCollection($oIE, "div")
    If IsObj($oDivs) Then
        For $oDiv In $oDivs
            If IsObj($oDiv) Then
                If $oDiv.className = "checkmark" And $oDiv.innerText = "Noch nicht veröffentlicht" Then
                    _IEAction($oDiv, "click")
                EndIf
            EndIf
        Next
    EndIf
EndIf

 

Thank you! Unfortunatly it does not peform a click, although I don't get an error. I think the problem is within the following part:

$oDiv.innerText = "Noch nicht veröffentlicht"

If I delete this part, it makes a click. But on the wrong/last object and obviously not on the "Noch nicht veröffentlicht" one. Maybe because "Noch nicht veröffentlicht" is written on a separate line in the DOM?

Link to comment
Share on other sites

2 hours ago, Subz said:

Try changing the line to:

If $oDiv.className = "checkmark" And StringStripWS($oDiv.innerText, 7) = "Noch nicht veröffentlicht" Then

Also use ConsoleWrite to show the text for example:
nb: This will show any white space between the "XXXX"

ConsoleWrite("XXXX" & $oDiv.innerText & "XXXX" & @CRLF)

 

Thanks for the suggestion, but it still did not do a click.

What should I expect from the ConsoleWrite? I got a loooooot of text written to the console.

$oDivs = _IETagNameGetCollection($oIE2, "div")
    If IsObj($oDivs) Then
        For $oDiv In $oDivs
            If IsObj($oDiv) Then
                If $oDiv.className = "checkmark" And StringStripWS($oDiv.innerText, 7) = "Noch nicht veröffentlicht" Then
                    _IEAction($oDiv, "click")
                 EndIf
               ConsoleWrite("XXXX" & $oDiv.innerText & "XXXX" & @CRLF)
            EndIf
        Next
    EndIf

At the moment it is looking like this. I am scared that I'll have to use a regular mouseclick. Save me!😃

Link to comment
Share on other sites

Link to comment
Share on other sites

1 hour ago, Nine said:

I think the problem is that the innertext is OUTSIDE the "checkmark".  The classname should be "item".  Modify it accordingly and add an ExitLoop after the click...

Yeeeaahhh! That did it!! Thank you so much:thumbsup:

Link to comment
Share on other sites

1 hour ago, _leo_ said:

Yeeeaahhh! That did it!! Thank you so much:thumbsup:

Man, you have found a way to do it without me giving you code, congrats...I am proud of you

Edited by Nine
Link to comment
Share on other sites

@_Leo_  Sorry didn't see that the checkmark div was closed, I need to update my glasses :blink:.  Just for future reference try posting the parent tags along with the item you're trying to manipulate, as it will help us to replicate the code, to help with the solution.

Link to comment
Share on other sites

On 7/20/2019 at 2:52 PM, Danp2 said:

Suggest that you post a larger segment of HTML or the URL, if possible.

Hey @Danp2

I figured out, that the website I am trying to automate is no longer working with IE. So I decided to rewriete my script to Webdriver UDF for Google Chrome. And since you seem like the "pro" with the wedriver, I'd love to ask for you help. ☺️

My new code is:

#include "wd_core.au3"
    #include "wd_helper.au3"
    
    Global $sSession, $sSession2
    Global $sDesiredCapabilities
    
     SetupChrome()
   _WD_Startup()
   
    $sSession = _WD_CreateSession($sDesiredCapabilities)
   _WD_Navigate($sSession, "url") 
   
   _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//*[text()='Noch nicht veröffentlicht']") ;Noch Nicht Veröffnetlicht
   $oNNV = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[(text(),'Noch nicht veröffentlicht')]")
   _WD_ElementAction($sSession, $oNNV, 'click')
   
   ;more code....

 

 Is it possible to click the element with the Webdriver? I am getting the following result: 

I thought the issue could be with the "ö", but it didnt work when I deleted "veröffentlicht".. Please let me know if you have any suggestions.

 

Quote

__WD_Post: URL=HTTP://127.0.0.1:9515/session/749bb00d8d317df99583768cc1b53d83/element; $sData={"using":"xpath","value":"//*[text()='Noch nicht veröffentlicht']"}
__WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"b5a42b27-819c-4b64-b917-795802d8c799"}}
_WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"b5a42b27-819c-4b64-b917-795802d8c799"}}
_WD_WaitElement ==> Success
__WD_Post: URL=HTTP://127.0.0.1:9515/session/749bb00d8d317df99583768cc1b53d83/element; $sData={"using":"xpath","value":"//*[(text(),'Noch nicht veröffentlicht')]"}
__WD_Post: StatusCode=400; ResponseText={"value":{"error":"invalid selector","message":"invalid selector: Unable to locate an element with the xpath expression //*[(text(),'Noch nicht veröffentlicht')] because of the following error:\nSyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[(text(),'Noch nicht veröffentlicht')]' is not a valid XPath expression.\n  (Session info: chrome=75.0.3770.142)","stacktrace":"Backtrace:\n\tOrdinal0 [0x01321B43+1448771]\n\tOrdinal0 [0x012A5A31+940593]\n\tOrdinal0 [0x01232293+467603]\n\tOrdinal0 [0x01233E03+474627]\n\tOrdinal0 [0x01233CF2+474354]\n\tOrdinal0 [0x011D624B+90699]\n\tOrdinal0 [0x011EF520+193824]\n\tOrdinal0 [0x011E7180+160128]\n\tOrdinal0 [0x011EE36B+189291]\n\tOrdinal0 [0x011E6FEB+159723]\n\tOrdinal0 [0x011D09F6+68086]\n\tOrdinal0 [0x011D1990+72080]\n\tOrdinal0 [0x011D1929+71977]\n\tGetHandleVerifier [0x01462E8C+1172172]\n\tGetHandleVerifier [0x013BFE55+504469]\n\tGetHandleVerifier [0x013BFBF0+503856]\n\tOrdinal0 [0x0132C9C8+1493448]\n\tGetHandleVerifier [0x013C062A+506474]\n\tOrdinal0 [0x012B7156+1012054]\n\tOrdinal0 [0x012B6FCF+1011663]\n\tOrdinal0 [0x012C135B+1053531]\n\tOrdinal0 [0x012C14C3+1053891]\n\tOrdinal0 [0x012C04F5+1049845]\n\tBaseThreadInitThunk [0x765D8494+36]\n\tRtlAreBitsSet [0x778C41C8+136]\n\tRtlAreBitsSet [0x778C4198+88]\n"}}
_WD_FindElement: {"value":{"error":"invalid selector","message":"invalid selector: Unable to locate an element with the xpath expression //*[(text(),'Noch nicht veröffentlicht')] because of the following error:\nSyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[(text(),'Noch nicht veröffentlicht')]' is not a valid XPath expression.\n  (Session info: chrome=75.0.3770.142)","stacktrace":"Backtrace:\n\tOrdinal0 [0x01321B43+1448771]\n\tOrdinal0 [0x012A5A31+940593]\n\tOrdinal0 [0x01232293+467603]\n\tOrdinal0 [0x01233E03+474627]\n\tOrdinal0 [0x01233CF2+474354]\n\tOrdinal0 [0x011D624B+90699]\n\tOrdinal0 [0x011EF520+193824]\n\tOrdinal0 [0x011E7180+160128]\n\tOrdinal0 [0x011EE36B+189291]\n\tOrdinal0 [0x011E6FEB+159723]\n\tOrdinal0 [0x011D09F6+68086]\n\tOrdinal0 [0x011D1990+72080]\n\tOrdinal0 [0x011D1929+71977]\n\tGetHandleVerifier [0x01462E8C+1172172]\n\tGetHandleVerifier [0x013BFE55+504469]\n\tGetHandleVerifier [0x013BFBF0+503856]\n\tOrdinal0 [0x0132C9C8+1493448]\n\tGetHandleVerifier [0x013C062A+506474]\n\tOrdinal0 [0x012B7156+1012054]\n\tOrdinal0 [0x012B6FCF+1011663]\n\tOrdinal0 [0x012C135B+1053531]\n\tOrdinal0 [0x012C14C3+1053891]\n\tOrdinal0 [0x012C04F5+1049845]\n\tBaseThreadInitThunk [0x765D8494+36]\n\tRtlAreBitsSet [0x778C41C8+136]\n\tRtlAreBitsSet [0x778C4198+88]\n"}}
_WD_FindElement ==> Webdriver Exception: HTTP status = 400
__WD_Post: URL=HTTP://127.0.0.1:9515/session/749bb00d8d317df99583768cc1b53d83/element//click; $sData={"id":""}
__WD_Post: StatusCode=404; ResponseText={"value":{"error":"no such element","message":"no such element: Element_id length is invalid\n  (Session info: chrome=75.0.3770.142)","stacktrace":"Backtrace:\n\tOrdinal0 [0x01321B43+1448771]\n\tOrdinal0 [0x012A5A31+940593]\n\tOrdinal0 [0x01232293+467603]\n\tOrdinal0 [0x011D5DA0+89504]\n\tOrdinal0 [0x011D7056+94294]\n\tOrdinal0 [0x011D2602+75266]\n\tOrdinal0 [0x011E714D+160077]\n\tOrdinal0 [0x011D23C6+74694]\n\tOrdinal0 [0x011E7361+160609]\n\tOrdinal0 [0x011EE36B+189291]\n\tOrdinal0 [0x011E6FEB+159723]\n\tOrdinal0 [0x011D09F6+68086]\n\tOrdinal0 [0x011D1990+72080]\n\tOrdinal0 [0x011D1929+71977]\n\tGetHandleVerifier [0x01462E8C+1172172]\n\tGetHandleVerifier [0x013BFE55+504469]\n\tGetHandleVerifier [0x013BFBF0+503856]\n\tOrdinal0 [0x0132C9C8+1493448]\n\tGetHandleVerifier [0x013C062A+506474]\n\tOrdinal0 [0x012B7156+1012054]\n\tOrdinal0 [0x012B6FCF+1011663]\n\tOrdinal0 [0x012C135B+1053531]\n\tOrdinal0 [0x012C14C3+1053891]\n\tOrdinal0 [0x012C04F5+1049845]\n\tBaseThreadInitThunk [0x765D8494+36]\n\tRtlAreBitsSet [0x778C41C8+136]\n\tRtlAreBitsSet [0x778C4198+88]\n"}}
_WD_ElementAction: {"value":{"error":"no such element","message":"no such element: Element_id length is invalid\n  (Ses...
_WD_ElementAction ==> Webdriver Exception: {"value":{"error":"no such element","message":"no such element: Element_id length is invalid\n  (Session info: chrome=75.0.3770.142)","stacktrace":"Backtrace:\n\tOrdinal0 [0x01321B43+1448771]\n\tOrdinal0 [0x012A5A31+940593]\n

 

Link to comment
Share on other sites

Thanks you for taking time! I appreciate it. @Network_Guy @Danp2

I tried both of your suggestions, but I keep getting weird errors. Like this:

Quote

 ==> Error parsing function call.:
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@class='item' and contains(text(), 'Noch nicht veröffentlicht')]"
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, ^ ERROR
>Exit code: 1    Time: 28.58

I dont know what is wrong with the ". I replaced it with single quotaition marks ', but then I got this error:

Quote

==> Unable to parse line.: 
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, '//div[@class='item' and contains(text(), 'Noch nicht veröffentlicht')]' 
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, '//div[@class='item' and contains(text(), 'Noch nicht ver^ ERROR
>Exit code: 1    Time: 0.3491

So I tried to remove the "veröffentlicht", and... tadaaaaaa... the next error.

Quote

 ==> Error parsing function call.:
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, '//div[@class='item' and contains(text(), 'Noch nicht')]'
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, '//div[@class='item' and contains(text(), 'Noch nicht^ ERROR

Thanks for helping!

Link to comment
Share on other sites

You clearly didn't read what he said:

5 hours ago, Network_Guy said:

$oNNV = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[(text(),'Noch nicht veröffentlicht')]")

 the comma in text(),'Noch' is the problem , use ' = ' as u did in _WD_WaitElement

 

You kept the comma in your script.

Change it to:

$oNNV = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[(text()='Noch nicht veröffentlicht')]")

 

Link to comment
Share on other sites

Thanks! @Davidowicza Ohh.. That's embarrassing😄

Unfortunatly I am getting the same error after changing:

Quote

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@class='item' and contains(text()='Noch nicht veröffentlicht')]"
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, ^ ERROR

so i guess there is still something invalid..

and with the following I am getting this: "//*[(text()='?Noch nicht veröffentlic?ht')]" even though there are no question marks in my code. so strange..

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//*[(text()='Noch nicht veröffentlicht')]") ;Noch Nicht Veröffnetlicht
   $oNNV = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[(text()='Noch nicht veröffentlicht')]")
   _WD_ElementAction($sSession, $oNNV, 'click')
Quote

__WD_Post: URL=HTTP://127.0.0.1:9515/session/ea5c17a97acf731ef72f9ba6bc9d49f4/element; $sData={"using":"xpath","value":"//*[(text()='?Noch nicht veröffentlic?ht')]"}
__WD_Post: StatusCode=404; ResponseText={"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\":\"xpath\",\"selector\":\"//*[(text()='?Noch nicht veröffentlic?ht')]\"}\n  (Session info: chrome=75.0.3770.142)","stacktrace":"Backtrace:\n\tOrdinal0 [0x013A1B43+1448771]\n\tOrdinal0 [0x01325A31+940593]\n\tOrdinal0 [0x012B2293+467603]\n\tOrdinal0 [0x01256463+91235]\n\tOrdinal0 [0x0126F520+193824]\n\tOrdinal0 [0x01267180+160128]\n\tOrdinal0 [0x0126E36B+189291]\n\tOrdinal0 [0x01266FEB+159723]\n\tOrdinal0 [0x012509F6+68086]\n\tOrdinal0 [0x01251990+72080]\n\tOrdinal0 [0x01251929+71977]\n\tGetHandleVerifier [0x014E2E8C+1172172]\n\tGetHandleVerifier [0x0143FE55+504469]\n\tGetHandleVerifier [0x0143FBF0+503856]\n\tOrdinal0 [0x013AC9C8+1493448]\n\tGetHandleVerifier [0x0144062A+506474]\n\tOrdinal0 [0x01337156+1012054]\n\tOrdinal0 [0x01336FCF+1011663]\n\tOrdinal0 [0x0134135B+1053531]\n\tOrdinal0 [0x013414C3+1053891]\n\tOrdinal0 [0x013404F5+1049845]\n\tBaseThreadInitThunk [0x765D8494+36]\n\tRtlAreBitsSet [0x778C41C8+136]\n\tRtlAreBitsSet [0x778C4198+88]\n"}}
_WD_FindElement: {"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\":\"xpath\",\"selector\":\"//*[(text()='?Noch nicht veröffentlic?ht')]\"}\n  (Session info: chrome=75.0.3770.142)","stacktrace":"Backtrace:\n\tOrdinal0 [0x013A1B43+1448771]\n\tOrdinal0 [0x01325A31+940593]\n\tOrdinal0 [0x012B2293+467603]\n\tOrdinal0 [0x01256463+91235]\n\tOrdinal0 [0x0126F520+193824]\n\tOrdinal0 [0x01267180+160128]\n\tOrdinal0 [0x0126E36B+189291]\n\tOrdinal0 [0x01266FEB+159723]\n\tOrdinal0 [0x012509F6+68086]\n\tOrdinal0 [0x01251990+72080]\n\tOrdinal0 [0x01251929+71977]\n\tGetHandleVerifier [0x014E2E8C+1172172]\n\tGetHandleVerifier [0x0143FE55+504469]\n\tGetHandleVerifier [0x0143FBF0+503856]\n\tOrdinal0 [0x013AC9C8+1493448]\n\tGetHandleVerifier [0x0144062A+506474]\n\tOrdinal0 [0x01337156+1012054]\n\tOrdinal0 [0x01336FCF+1011663]\n\tOrdinal0 [0x0134135B+1053531]\n\tOrdinal0 [0x013414C3+1053891]\n\tOrdinal0 [0x013404F5+1049845]\n\tBaseThreadInitThunk [0x765D8494+36]\n\tRtlAreBitsSet [0x778C41C8+136]\n\tRtlAreBitsSet [0x778C4198+88]\n"}}
_WD_FindElement ==> No match: HTTP status = 404

 

 

 

 

Edited by _leo_
Link to comment
Share on other sites

Thanks! I got the syntax error out of the way after rewriting 100 times, restarting autoit, and rebooting... 

Everything would work fine.. but I am getting a message that the element is not clickable. I figured out, that there is an other element with the same text name and the script tries to refer to the wrong one. 

Quote

"error":"element click intercepted","message":"element click intercepted: Element \u003Cspan data-v-7abc0274=\"\" class=\"text-truncate\">...\u003C/span> is not clickable at point

 

So i guess I have to get the classname in there aswell, to identify it completly (with the suggestion from @Danp2.) Therefore my code looks like this. And somehow the previous error is gone, but a new Syntax error has occured.

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@class='item' and contains(text()='Noch nicht veröffentlicht')]")
    $NNV = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@class='item' and contains(text()='Noch nicht veröffentlicht')]")
    _WD_ElementAction($sSession, $NNV, 'click')
Quote

_WD_FindElement: {"value":{"error":"invalid selector","message":"invalid selector: Unable to locate an element with the xpath expression //div[@class='item' and contains(text()='Noch nicht veröffentlicht')] because of the following error:\nSyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[@class='item' and contains(text()='Noch nicht veröffentlicht')]' is not a valid XPath expression.\n  (Session info: chrome=75.0.3770.142)","stacktrace":"Backtrace:\n\tOrdinal0 

I read this line a million times. I dont see a syntax mistake... Please someone buy me new eyes🧐

Link to comment
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
 Share

×
×
  • Create New...