Iteration using Xpath

Hello,

I’m trying to use the Craiglist bot script but instead of the Craiglist listing page, a local listing page called ClasificadosOnline.com.

Using the same logic as the Craiglist bot, I used the following xpaths:

Do you have any errors when you run this script?

The message is the following:

Error executing WebElementAction
com.workfusion.studio.rpa.recorder.playback.PlaybackException: Error executing TemplateAction[templateName=WebElementAction.ftl,id=19,name=Optional[WebElementAction],parent=17,nextSibling=20,arguments=ActionArguments[varName=[item_name],delay=[0],exactMatch=[true],useElementAttribute=[false],timeoutMs=[10000],webElementLocatorType=[XPATH],xsi:type=[recorder:WebElementAction, recorder:WebElementAction],pollingInterval=[300],active=[true],searchInFrames=[false],webElementLocator=[//span[@class=“Tahoma15blacknound”][${count}]],awaitTimeout=[5000]]]
at com.workfusion.studio.rpa.recorder.playback.flow.StandardControlFlow.execute(StandardControlFlow.java:54)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:28)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:15)
at com.workfusion.studio.rpa.recorder.playback.player.ActionPlayer.next(ActionPlayer.java:81)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.playNextAction(PlaybackLogic.java:154)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.run(PlaybackLogic.java:112)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: //span[@class=“Tahoma15blacknound”][2]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘9.3.0.5’, revision: ‘63553f3f1c’, time: ‘2019-03-15T13:41:27.26Z’
System info: host: ‘WIN-FO6O2IMUE4L’, ip: ‘172.17.3.34’, os.name: ‘Windows Server 2008 R2’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_121’
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{moz:profile=D:\Users\jmangual\AppData\Local\Temp\rust_mozprofile.3dGHizZx8l2Y, rotatable=false, moz:geckodriverVersion=0.22.0, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, unhandledPromptBehavior=dismiss and notify, strictFileInteractability=false, moz:headless=false, platform=XP, moz:accessibilityChecks=false, moz:useNonSpecCompliantPointerOrigin=false, acceptInsecureCerts=false, browserVersion=66.0.5, moz:shutdownTimeout=60000, platformVersion=6.1, moz:processID=10340, browserName=firefox, javascriptEnabled=true, platformName=XP, setWindowRect=true, moz:webdriverClick=true}]
Session ID: 3514336a-045b-4773-a05a-6a552deca5e0
*** Element info: {Using=xpath, value=//span[@class=“Tahoma15blacknound”][2]}
Command duration or timeout: 0 milliseconds
at sun.reflect.GeneratedConstructorAccessor102.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:41)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:741)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:446)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:548)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:438)
at com.workfusion.rpa.helpers.RPA.getElement(RPA.java:944)
at com.workfusion.rpa.helpers.RPA.$(RPA.java:864)
at sun.reflect.GeneratedMethodAccessor40.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:104)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:102)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:209)
at Script16.run(Script16.groovy:9)
at com.workfusion.studio.rpa.recorder.playback.shell.GroovyShellWrapper.executeScript(GroovyShellWrapper.java:48)
at com.workfusion.studio.rpa.recorder.playback.player.PlaybackContext.executeScript(PlaybackContext.java:108)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.executeBehavior(TemplateAction.java:33)
at com.workfusion.studio.rpa.recorder.playback.flow.StandardControlFlow.execute(StandardControlFlow.java:46)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:28)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:15)
at com.workfusion.studio.rpa.recorder.playback.player.ActionPlayer.next(ActionPlayer.java:81)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.playNextAction(PlaybackLogic.java:154)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.run(PlaybackLogic.java:112)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: //span[@class=“Tahoma15blacknound”][2]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘9.3.0.5’, revision: ‘63553f3f1c’, time: ‘2019-03-15T13:41:27.26Z’
System info: host: ‘WIN-FO6O2IMUE4L’, ip: ‘172.17.3.34’, os.name: ‘Windows Server 2008 R2’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_121’
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{moz:profile=D:\Users\jmangual\AppData\Local\Temp\rust_mozprofile.3dGHizZx8l2Y, rotatable=false, moz:geckodriverVersion=0.22.0, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, unhandledPromptBehavior=dismiss and notify, strictFileInteractability=false, moz:headless=false, platform=XP, moz:accessibilityChecks=false, moz:useNonSpecCompliantPointerOrigin=false, acceptInsecureCerts=false, browserVersion=66.0.5, moz:shutdownTimeout=60000, platformVersion=6.1, moz:processID=10340, browserName=firefox, javascriptEnabled=true, platformName=XP, setWindowRect=true, moz:webdriverClick=true}]
Session ID: 3514336a-045b-4773-a05a-6a552deca5e0
*** Element info: {Using=xpath, value=//span[@class=“Tahoma15blacknound”][2]}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘9.3.0.5’, revision: ‘63553f3f1c’, time: ‘2019-03-15T13:41:27.26Z’
System info: host: ‘WIN-FO6O2IMUE4L’, ip: ‘172.17.3.34’, os.name: ‘Windows Server 2008 R2’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_121’
Driver info: driver.version: UniversalDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:186)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:121)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:50)
at org.openqa.selenium.remote.server.ActiveSessionCommandExecutor.execute(ActiveSessionCommandExecutor.java:43)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:741)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:446)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:548)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:438)
at com.workfusion.universal.driver.UniversalDriver.findElement(UniversalDriver.java:80)
at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:48)
at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:33)
at org.openqa.selenium.remote.server.handler.WebDriverHandler.handle(WebDriverHandler.java:41)
at org.openqa.selenium.remote.server.rest.ResultConfig.handle(ResultConfig.java:134)
at org.openqa.selenium.remote.server.JsonHttpCommandHandler.handleRequest(JsonHttpCommandHandler.java:206)
at org.openqa.selenium.remote.server.InMemorySession.execute(InMemorySession.java:98)
at org.openqa.selenium.remote.server.WebDriverServlet.lambda$handle$0(WebDriverServlet.java:231)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Any solution?

@julio_mangual the script you sent is not the one on the screenshot above, so I cannot test it.

Check the xpath from the action that fails in the Chrome Dev Tools. It might be wrong.

Perhaps, you need this xpath: (//span[@class=“Tahoma15blacknound”])[2]

Im sorry, I didn’t attached the correct one.

Clasificadosonline (2).zip (11.3 KB)

1 Like

Thank you. I’ll have a look.

You need to use use brackets in the Xpath: (//span[@class=“Tahoma15blacknound”])[${count}]

You can read more about working with lists of elements in the XPath guide:
https://kb.workfusion.com/display/RPAe/XPath+Guide#XPathGuide-ListsofElements

It’s working beautifully. My last question is that is not exporting the link of the listing into excel. Anything wrong in the script?

It is because you have limited the range with column D.

image

You can delete the end cell from this action altogether so the end coordinates of the range will be dynamic.

One last question and Im done, you’ve been more than helpful.

If I want this loop to be repeated page after page to gather all the data, should I use a While or an If? What do you recommend?

Hello! Thanks so much for your help. Following my question above.

I guess you could use the While loop.
At the end of the loop, press the button to go the next page, and put this action in the Exception handling. If it is the last page and the button doesn’t exist so the bot cannot press, program it to set a certain value to a variable, for example, set boolean variable ${button_exists} to “false”.

Have the condition in the While loop: while ${button_exists} equals “true”.

Hope it helps!