'Getting WebDriverSampler: Cannot cast org.openqa.selenium.remote.RemoteWebElement to org.openqa.selenium.By error

When I try running my jmeter webdriver sampler with the below code I am getting the error message as shown below.

 var selenium = JavaImporter(org.openqa.selenium)
 var time = JavaImporter(java.util.concurrent.TimeUnit)
 WDS.browser.manage().timeouts().implicitlyWait(30, time.TimeUnit.SECONDS)
 WDS.sampleResult.sampleStart()

 WDS.log.info("Enter email batch details");
 var batchNameText = WDS.browser.findElement(selenium.By.xpath("//div[contains(@data-testid,\"batchNameTextField\")]//input"))
    wait.until(EC.presenceOfElementLocated(batchNameText));
    batchNameText.click();
    batchNameText.clear();
    batchNameText.sendKeys("test");
    batchNameText.SendKeys(selenium.Keys.TAB);

 WDS.sampleResult.sampleEnd()

Error in log file shows as

java.lang.ClassCastException: Cannot cast org.openqa.selenium.remote.RemoteWebElement to org.openqa.selenium.By at java.lang.invoke.MethodHandleImpl.newClassCastException(Unknown Source) at java.lang.invoke.MethodHandleImpl.castReference(Unknown Source) at jdk.nashorn.internal.scripts.Script$5$^eval_.:program( :39) at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637) at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494) at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406) at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402) at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155) at javax.script.AbstractScriptEngine.eval(Unknown Source) at com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler.sample(WebDriverSampler.java:86) at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) at java.lang.Thread.run(Unknown Source)

Not sure whats the issue here. Its same as getting webelement and assigning it to a variable as what I guess.

Thanks in advance.



Solution 1:[1]

Looking at presenceOfElementLocated? function JavaDoc it expects By class instance and you're passing a WebElement to it that's why your code fails.

So my expectation is that you need to change it to something like:

wait.until(EC.presenceOfElementLocated(selenium.By.xpath("//div[contains(@data-testid,\"batchNameTextField\")]//input"))

Going forward please include the full code because it's not very clear what is EC as it isn't declared anywhere.

More information on WebDriver Sampler: The WebDriver Sampler: Your Top 10 Questions Answered

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T