{"id":3729,"date":"2017-07-26T06:56:48","date_gmt":"2017-07-26T06:56:48","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3729"},"modified":"2024-09-04T07:25:40","modified_gmt":"2024-09-04T07:25:40","slug":"how-to-fix-java-lang-classcastexception-class-org-openqa-selenium-bybycssselector-cannot-be-cast-to-class-org-openqa-selenium-webelement","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-fix-java-lang-classcastexception-class-org-openqa-selenium-bybycssselector-cannot-be-cast-to-class-org-openqa-selenium-webelement\/","title":{"rendered":"How to fix java.lang.ClassCastException."},"content":{"rendered":"<h2>How to fix java.lang.ClassCastException.<\/h2>\n<p>In this tutorial, we will fix a Selenium test that fails with the exception java.lang.ClassCastException: class org.openqa.selenium.By$ByCssSelector cannot be cast to class org.openqa.selenium.WebElement.<\/p>\n<h3>Error Trace<\/h3>\n<pre>java.lang.ClassCastException: class org.openqa.selenium.By$ByCssSelector cannot \r\nbe cast to class org.openqa.selenium.WebElement \r\n(org.openqa.selenium.By$ByCssSelector and org.openqa.selenium.WebElement\r\n are in unnamed module of loader 'app')\r\n\r\n        at com.testingdocs.sample.AppTest.firstTest(AppTest.java:16)\r\n        at java.base\/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(\r\nNative Method)\r\n        at java.base\/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(\r\nNativeMethodAccessorImpl.java:62)\r\n        at java.base\/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(\r\nDelegatingMethodAccessorImpl.java:43)\r\n        at java.base\/java.lang.reflect.Method.invoke(Method.java:564)\r\n        at org.testng.internal.MethodInvocationHelper.invokeMethod(\r\nMethodInvocationHelper.java:134)\r\n        at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)\r\n        at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)\r\n        at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)\r\n        at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(\r\nTestInvoker.java:816)\r\n        at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)\r\n        at org.testng.internal.TestMethodWorker.invokeTestMethods(\r\nTestMethodWorker.java:146)\r\n        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)\r\n        at java.base\/java.util.ArrayList.forEach(ArrayList.java:1510)\r\n        at org.testng.TestRunner.privateRun(TestRunner.java:766)\r\n        at org.testng.TestRunner.run(TestRunner.java:587)\r\n        at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)\r\n        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)\r\n        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)\r\n        at org.testng.SuiteRunner.run(SuiteRunner.java:286)\r\n        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)\r\n        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)\r\n        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)\r\n        at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)\r\n        at org.testng.TestNG.runSuites(TestNG.java:1039)\r\n        at org.testng.TestNG.run(TestNG.java:1007)\r\n        at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)\r\n        at com.intellij.rt.testng.RemoteTestNGStarter.main(\r\nRemoteTestNGStarter.java:110)<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3735\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Failed-Test.png\" alt=\"Selenium Failed Test\" width=\"1848\" height=\"907\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Failed-Test.png 1848w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Failed-Test-300x147.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Failed-Test-1024x503.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Failed-Test-768x377.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Failed-Test-1536x754.png 1536w\" sizes=\"auto, (max-width: 1848px) 100vw, 1848px\" \/><\/p>\n<h3>Buggy Code<\/h3>\n<pre><\/pre>\n<pre>package com.testingdocs.sample;\r\nimport org.openqa.selenium.By;\r\nimport org.openqa.selenium.Keys;\r\nimport org.openqa.selenium.WebDriver;\r\nimport org.openqa.selenium.WebElement;\r\nimport org.openqa.selenium.firefox.FirefoxDriver;\r\nimport org.testng.Assert;\r\nimport org.testng.annotations.Test;\r\n\r\npublic class AppTest {\r\n    @Test\r\n    public void firstTest() {\r\n        WebDriver driver = new FirefoxDriver();\r\n        try {\r\n            driver.get(\"http:\/\/localhost\");\r\n            WebElement result = (WebElement)By.cssSelector(\"div.page_header\");\r\n            Assert.assertEquals(result.getText(),\"Apache2 Ubuntu Default Page\");\r\n        } finally {\r\n            driver.quit();\r\n        }\r\n    }\r\n}<\/pre>\n<pre><\/pre>\n<h3><\/h3>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3736\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Test-Debug-mode.png\" alt=\"Selenium Test Debug mode\" width=\"1860\" height=\"903\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Test-Debug-mode.png 1860w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Test-Debug-mode-300x146.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Test-Debug-mode-1024x497.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Test-Debug-mode-768x373.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Selenium-Test-Debug-mode-1536x746.png 1536w\" sizes=\"auto, (max-width: 1860px) 100vw, 1860px\" \/><\/h3>\n<h3><\/h3>\n<h3>Fix<\/h3>\n<p>We can take the clue from the exception trace on which line you get the exception. If you figure out the error it&#8217;s ok. In many cases, we may need to debug the test to find the actual cause of the exception.<\/p>\n<pre>WebElement result = (WebElement)By.cssSelector(\"div.page_header\");<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p>Many automation engineers just try to typecast the locating finding technique to the class of the WebElement. This is error-prone and would result in <strong>java.lang.ClassCastException. <\/strong>The correct approach is to use the <strong>driver.findElement()<\/strong> method.<\/p>\n<p>&nbsp;<\/p>\n<pre>WebElement result = driver.findElement(By.cssSelector(\"div.page_header\"));<\/pre>\n<h3><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>How to fix java.lang.ClassCastException. In this tutorial, we will fix a Selenium test that fails with the exception java.lang.ClassCastException: class org.openqa.selenium.By$ByCssSelector cannot be cast to class org.openqa.selenium.WebElement. Error Trace java.lang.ClassCastException: class org.openqa.selenium.By$ByCssSelector cannot be cast to class org.openqa.selenium.WebElement (org.openqa.selenium.By$ByCssSelector and org.openqa.selenium.WebElement are in unnamed module of loader &#8216;app&#8217;) at com.testingdocs.sample.AppTest.firstTest(AppTest.java:16) at java.base\/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0( Native Method) at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[],"class_list":["post-3729","post","type-post","status-publish","format-standard","hentry","category-selenium","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3729"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3729\/revisions"}],"predecessor-version":[{"id":24080,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3729\/revisions\/24080"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}