PnD Posted June 18, 2021 Share Posted June 18, 2021 Dear all, I have successfully written a script to read a certain email with a specific matched title with IE without any problem at all. Below is an example that I can be able to read only email with Title "Main Server Room" Local $oTitle = _IETagNameGetCollection($body, "div") For $title in $oTitle If StringInStr(($title.title),"Main Server Room") > 0 Then _IEAction($title, "focus") _IEAction($title, "click") EndIf Next I have read all over the WebDriver wiki page and could not be able to find anything similar to the function _IETagNameGetCollection. Do you have any suggestion that I can achieve the same goal with WebDriver on either Firefox or Chrome. Your feedback is much appreciated. Link to comment Share on other sites More sharing options...
Danp2 Posted June 18, 2021 Share Posted June 18, 2021 You should be able to use _WD_FindElement with the correct selector to locate the desired element(s). Can the target email appear more than once in the webpage? PnD 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
PnD Posted June 18, 2021 Author Share Posted June 18, 2021 Hi Danp2, Thank you so much for getting back to me so quickly. The email appears to be more than once and that's why the loop is very helpful. I did try the _WD_FindElement but the xpath only show //div[@id='row:7:294307'] and this row is dynamic depending on how many email I have in my inbox and I cannot use it for this purpose. The best way for me is to rely on the title. For example, If i search my email for "Main Server Room", the result that I get will be 5 email, with the loop above, I can easily read all 5 of them. Link to comment Share on other sites More sharing options...
Danp2 Posted June 18, 2021 Share Posted June 18, 2021 Ok... How about something like this? $aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[[contains(text(),'Main Server Room')]]", Default, True) _ArrayDisplay($aElements, "Found Elements") Once you have the array of elements, you can loop through and process each one. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
PnD Posted June 18, 2021 Author Share Posted June 18, 2021 hi Danp2, Sorry for the late response. I followed your suggestion but it seems the selector is invalid as showed in logs below __WD_Post: URL=HTTP://127.0.0.1:4444/session/4fe91fb8-0cc3-4c06-83b2-2ef5f9eeb560/element; $sData={"using":"xpath","value":"//input[@id='messageSearch']"} __WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"18d995e0-d284-4f4c-b162-88ef5788959d"}}... _WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"18d995e0-d284-4f4c-b162-88ef5788959d"}} __WD_Post: URL=HTTP://127.0.0.1:4444/session/4fe91fb8-0cc3-4c06-83b2-2ef5f9eeb560/element; $sData={"using":"xpath","value":"//i[@class='icon-search pointer-cursor']"} __WD_Post: StatusCode=200; ResponseText={"value":{"element-6066-11e4-a52e-4f735466cecf":"bf2690c6-7701-4a29-8214-8945d995c783"}}... _WD_FindElement: {"value":{"element-6066-11e4-a52e-4f735466cecf":"bf2690c6-7701-4a29-8214-8945d995c783"}} __WD_Post: URL=HTTP://127.0.0.1:4444/session/4fe91fb8-0cc3-4c06-83b2-2ef5f9eeb560/element/18d995e0-d284-4f4c-b162-88ef5788959d/clear; $sData={"id":"18d995e0-d284-4f4c-b162-88ef5788959d"} __WD_Post: StatusCode=200; ResponseText={"value":null}... _WD_ElementAction: {"value":null}... __WD_Post: URL=HTTP://127.0.0.1:4444/session/4fe91fb8-0cc3-4c06-83b2-2ef5f9eeb560/element/18d995e0-d284-4f4c-b162-88ef5788959d/value; $sData={"id":"18d995e0-d284-4f4c-b162-88ef5788959d", "text":"Main Server Room"} __WD_Post: StatusCode=200; ResponseText={"value":null}... _WD_ElementAction: {"value":null}... __WD_Post: URL=HTTP://127.0.0.1:4444/session/4fe91fb8-0cc3-4c06-83b2-2ef5f9eeb560/element/bf2690c6-7701-4a29-8214-8945d995c783/click; $sData={"id":"bf2690c6-7701-4a29-8214-8945d995c783"} __WD_Post: StatusCode=200; ResponseText={"value":null}... _WD_ElementAction: {"value":null}... __WD_Post: URL=HTTP://127.0.0.1:4444/session/4fe91fb8-0cc3-4c06-83b2-2ef5f9eeb560/execute/sync; $sData={"script":"return document.body.innerText;", "args":[{}]} __WD_Post: StatusCode=200; ResponseText={"value":" Sanjose \n\nEMAIL\n\nCALENDAR\n\nCONTACTS\n\nTASKS\n\nNOTES\n\nDOCS\n\nVOICE\n\n New Em... _WD_ExecuteScript: {"value":" Sanjose \n\nEMAIL\n\nCALENDAR\n\nCONTACTS\n\nTASKS\n\nNOTES\n\nDOCS\n\nVOICE\n\n New Em... __WD_Post: URL=HTTP://127.0.0.1:4444/session/4fe91fb8-0cc3-4c06-83b2-2ef5f9eeb560/elements; $sData={"using":"xpath","value":"//div[contains(text(),'Main Server Room']"} __WD_Post: StatusCode=400; ResponseText={"value":{"error":"invalid selector","message":"Given xpath expression \"//div[contains(text(),'Main... __WD_Post ==> Webdriver Exception: {"value":{"error":"invalid selector","message":"Given xpath expression \"//div[contains(text(),'Main Server Room']\" is invalid: SyntaxError: The expression is not a legal expression.","stacktrace":"WebDriverError@chrome://marionette/content/error.js:181:5\nInvalidSelectorError@chrome://marionette/content/error.js:341:5\nfind_@chrome://marionette/content/element.js:334:11\nelement.find/</findElements<@chrome://marionette/content/element.js:288:24\nevalFn@chrome://marionette/content/sync.js:136:7\nPollPromise/<@chrome://marionette/content/sync.js:156:5\nPollPromise@chrome://marionette/content/sync.js:127:10\nelement.find/<@chrome://marionette/content/element.js:286:24\nelement.find@chrome://marionette/content/element.js:285:10\nfindElements@chrome://marionette/content/actors/MarionetteCommandsChild.jsm:241:20\nreceiveMessage@chrome://marionette/content/actors/MarionetteCommandsChild.jsm:94:31\n"}} _WD_FindElement: {"value":{"error":"invalid selector","message":"Given xpath expression \"//div[contains(text(),'Main Server Room']\" is invalid: SyntaxError: The expression is not a legal expression.","stacktrace":"WebDriverError@chrome://marionette/content/error.js:181:5\nInvalidSelectorError@chrome://marionette/content/error.js:341:5\nfind_@chrome://marionette/content/element.js:334:11\nelement.find/</findElements<@chrome://marionette/content/element.js:288:24\nevalFn@chrome://marionette/content/sync.js:136:7\nPollPromise/<@chrome://marionette/content/sync.js:156:5\nPollPromise@chrome://marionette/content/sync.js:127:10\nelement.find/<@chrome://marionette/content/element.js:286:24\nelement.find@chrome://marionette/content/element.js:285:10\nfindElements@chrome://marionette/content/actors/MarionetteCommandsChild.jsm:241:20\nreceiveMessage@chrome://marionette/content/actors/MarionetteCommandsChild.jsm:94:31\n"}} _WD_FindElement ==> Webdriver Exception: HTTP status = 400 +>14:35:41 AutoIt3.exe ended.rc:0 +>14:35:41 AutoIt3Wrapper Finished. >Exit code: 0 Time: 21.26 Originally, it was $aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[[contains(text(),'Main Server Room')]]", Default, True) and I changed it to $aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[contains(text(),'Main Server Room')]", Default, True) and it's still invalid. Here is my script as well $inbox = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//input[@id='messageSearch']") $click = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//i[@class='icon-search pointer-cursor']") $PO = "Main Server Room" _WD_ElementAction($oFF, $inbox,'Clear') _WD_ElementAction($oFF, $inbox, 'value',$PO) _WD_ElementAction($oFF, $click, 'Click') Sleep(2000) $getBody= _WD_ExecuteScript($oFF,"return document.body.innerText;", '{}') ;MsgBox(0,0,$getBody) If StringInStr($getBody,$PO) > 0 Then ;msgbox(0,0,"Look Ok") $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(text(),'Main Server Room']", Default, True) _ArrayDisplay($aElements) EndIf Also, I would like to ask that when you used the function text(), would that work if the subject contains at least this term "Main Server Room" . For example: Subjects of Email returned from the search result for "Main Server Room", may be: 1. This is the Main Server Room 2. Main Server Room Temperature 3. The Main Server Room is too hot 4. Main Server Room and so on.... and $aElements will be showing off in the Array for as many times as "Main Server Room" is detected. Link to comment Share on other sites More sharing options...
Danp2 Posted June 18, 2021 Share Posted June 18, 2021 Right. the double brackets was a copy / paste issue on my part. Not sure why the xpath isn't working TBH. Maybe text() isn't valid with a Div. Try changing it to this -- $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(.,'Main Server Room']", Default, True) If that works, then you should be able to find the answer to your other question. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
PnD Posted June 18, 2021 Author Share Posted June 18, 2021 Hi Danp2, Thank you for your quick fix. Your suggested code return invalid selector again , so I changed it to $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(.,'Main Server Room')]", Default, True) This works right away. However, the result (showing 18 $aElements instead of 3) seems to be incorrect as from my search result, I only have three emails, and actually those are the only three email that have the subject containing "Main Server Room" I have in my inbox as well. Do you have any idea what may cause this issue? Link to comment Share on other sites More sharing options...
PnD Posted June 18, 2021 Author Share Posted June 18, 2021 (edited) hahah, I think your first solution works. It just missed a bracket at the end of 'Main Server Room' So this works beautifully $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(text(),'Main Server Room')]", Default, True) Once again, thank you so much for your kind help. Edited June 18, 2021 by PnD Link to comment Share on other sites More sharing options...
Solution Danp2 Posted June 18, 2021 Solution Share Posted June 18, 2021 Try this instead -- $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(@title,'Main Server Room')]", Default, True) Formatting should be good cause I just modified your version. 😄 PnD 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
PnD Posted June 18, 2021 Author Share Posted June 18, 2021 4 minutes ago, Danp2 said: Try this instead -- $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(@title,'Main Server Room')]", Default, True) Formatting should be good cause I just modified your version. 😄 and $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(text(),'Main Server Room')]", Default, True) works beautifully as well. Thank you so much Dan! Link to comment Share on other sites More sharing options...
PnD Posted June 18, 2021 Author Share Posted June 18, 2021 Hi Danp2, I ran into a little issue when parsing a variable inside these codes "//div[contains(text(),$PO)]" or "//div[contains(@title,$PO)]" or "//div[contains(@title,"$PO")]" or "//div[contains(text(),"$PO")]" and they are all returned error: syntax error would you please give me some idea how to fix this.? $PO= "Main Server Room" Link to comment Share on other sites More sharing options...
Danp2 Posted June 18, 2021 Share Posted June 18, 2021 You need to build the string by concatenating the variable -- $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(@title,'" & $PO & "')]", Default, True) Untested, but you should get the idea from the above. PnD 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
PnD Posted June 19, 2021 Author Share Posted June 19, 2021 57 minutes ago, Danp2 said: You need to build the string by concatenating the variable -- $aElements = _WD_FindElement($oFF, $_WD_LOCATOR_ByXPath, "//div[contains(@title,'" & $PO & "')]", Default, True) Untested, but you should get the idea from the above. Thank you Dan! This works perfectly. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now