java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement when executing test through Selenium
java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement when executing test through Selenium
I'm trying make an automation test in Selenium WebDriver, but Eclipse is showing me the following error:
java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement
Here is my code:
public void SeachApp()
Object elem = driver.getPageSource().contains("Staffing");
if (elem != null)
System.out.println("Encontrado");
int width = ((WebElement) elem).getSize().getWidth();
Actions act = new Actions(driver);
act.moveToElement((WebElement) elem)
.moveByOffset((width / 2) - 15, 0)
.click()
.perform();
else
System.out.println("Não Encontrado");
What's going on, and how can I fix this?
driver.getPageSource()
driver.getPageSource().contains("Staffing")
I need to click on a WebElement that has the ID dynamic, so I will search by text Object elem = driver.getPageSource().contains("Staffing") and after I'll try to get it position on Web page so click.
– Cleicy Guião
Aug 22 at 18:45
@DebanjanB explained good, check it out
– sers
Aug 22 at 18:55
1 Answer
1
As per the documentation getPageSource()
returns the source of the current page and is defined as:
getPageSource()
Get the source of the last loaded page. If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page. You need to follow the documentation of the particular driver being used to determine whether the returned text reflects the current state of the page or the text last sent by the web server. The page source returned is a representation of the underlying DOM: do not expect it to be formatted or escaped in the same way as the response sent from the web server.
The contains()
method is a Java method to check if the String contains another substring or not. It returns boolean value so it can use directly inside if statements.
contains()
The expression which you have used:
driver.getPageSource().contains("Staffing");
will return a Boolean Value but gets casted into an Object
type of object. Further, as you are trying to cast an Object
type of object to WebElement
type you see the error as:
Object
Object
WebElement
java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement
If you desire to locate a WebElement you have to use either of the following methods:
findElement()
findElements
Now, if you desire to get hold of the WebElement with text as Staffing (without any reference to the relevant HTML) you can use the following solution:
WebElement element = driver.findElement(By.xpath("//*[contains(text(),'Staffing')]"));
Now, once you located the WebElement, you can easily perform the remaining of your tasks with the WebElement.
Thank you so much! =) It works for me
– Cleicy Guião
Aug 22 at 19:51
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
driver.getPageSource()
return you String anddriver.getPageSource().contains("Staffing")
return you boolean not WebElement, you cannot cast it to WebElement. What are you trying to achieve here?– sers
Aug 22 at 18:16