Get xpath count within custom keyword

I want to get xpath count in the relevant page.
I have used the following code. but it giving an error. please help on this.

Code :

WebDriver driver = DriverFactory.getWebDriver()

def List < WebElement > eleCount = driver.findElement(By.xpath(“//li[@class='ad-listitem lazyload-item ']//h2”))

println eleCount

_int resultCount = eleCount.size();

_

NOTE : please refer attached screenshot for complete code.

**Error : **

[ERROR] - Cannot cast object '[[CChromeDriver: chrome on XP (7aec31b23cdc72cfecd9a232999a206e)] → xpath: //li[@class='ad-listitem lazyload-item ‘]//h2]’ with class ‘org.openqa.selenium.remote.RemoteWebElement’ to class ‘java.util.List’

[END] - End action : helper.searchKeyWords.searchKeyWord

[ERROR] - Test Cases/tc_SearchKeyWord FAILED because (of) org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[[CChromeDriver: chrome on XP (7aec31b23cdc72cfecd9a232999a206e)] → xpath: //li[@class='ad-listitem lazyload-item ‘]//h2]’ with class ‘org.openqa.selenium.remote.RemoteWebElement’ to class ‘java.util.List’

_[END] - End Test Case : Test Cases/tc_SearchKeyWord


_

getObjectCountCode.PNG

errorgetXpathCount.PNG

List eleCount = driver.findElements(By.xpath("//li[@listitem lazyload-item ']//h2")) :slight_smile:

Marek Melocik said:

List eleCount = driver.findElements(By.xpath("//li[@listitem lazyload-item ']//h2")) :slight_smile:

Thanks for the quick response. but not it is giving some other error. I’m fed up with this matter :frowning: please help on this. :frowning:

Error :

07-27-2018 09:39:31 PM - [ERROR] - invalid selector: Unable to locate an element with the xpath expression //li[@listitem lazyload-item ']//h2 because of the following error:

SyntaxError: Failed to execute ‘evaluate’ on ‘Document’: The string '//li[@listitem lazyload-item ‘]//h2’ is not a valid XPath expression.

(Session info: chrome=67.0.3396.99)

(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 0 milliseconds

For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html

Build info: version: ‘3.7.1’, revision: ‘8a0099a’, time: ‘2017-11-06T21:07:36.161Z’

System info: host: ‘CSISL_SUDAM’, ip: ‘100.67.15.46’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_102’

Driver info: com.kms.katalon.selenium.driver.CChromeDriver

Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.35.528161 (5b82f2d2aae0ca…, userDataDir: C:\Users\Dell\AppData\Local…}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.99, webStorageEnabled: true}

Session ID: e9d5506a33292ea6e11d623d5b1c6762

*** Element info: {Using=xpath, value=//li[@listitem lazyload-item ']//h2}

07-27-2018 09:39:31 PM - [END] - End action : helper.searchKeyWords.searchKeyWord

07-27-2018 09:39:31 PM - [ERROR] - Test Cases/tc_SearchKeyWord FAILED because (of) org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //li[@listitem lazyload-item ']//h2 because of the following error:

SyntaxError: Failed to execute ‘evaluate’ on ‘Document’: The string '//li[@listitem lazyload-item ‘]//h2’ is not a valid XPath expression.

(Session info: chrome=67.0.3396.99)

(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 0 milliseconds

For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html

Build info: version: ‘3.7.1’, revision: ‘8a0099a’, time: ‘2017-11-06T21:07:36.161Z’

System info: host: ‘CSISL_SUDAM’, ip: ‘100.67.15.46’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_102’

Driver info: com.kms.katalon.selenium.driver.CChromeDriver

Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.35.528161 (5b82f2d2aae0ca…, userDataDir: C:\Users\Dell\AppData\Local…}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.99, webStorageEnabled: true}

Session ID: e9d5506a33292ea6e11d623d5b1c6762

*** Element info: {Using=xpath, value=//li[@listitem lazyload-item ']//h2}

Looks like you have incorrect Xpath.

'//li[@listitem lazyload-item   ']//h2'

Please doublecheck it.

Looks like you have incorrect XPath

You are right, but it is because the software of the Katalon Forum has a problem dealing with @ characters in the comment text. Any comment with @ is garbled. Especially XPath expression tends to be rewritten wrong by the forum. It’s a shame.

-------------

sudam,

I could find your real xpath in the screenshot you attached. OK. I would suggest to you to use XPath contains() function.

スクリーンショット 2018-07-28 6.59.42.png

スクリーンショット 2018-07-28 7.30.37.png

1 Like

Hi All,
Thanks for all the responses. :slight_smile:
Wrong was with my XPath. it is not recognized the given XPath. but it recognized from webdriver. :frowning: