问题
I am trying to automate resize a column from a table, by using Actions class this is my code:
public boolean resizeColumn(String columnToResize, String toColumn) {
try {
WebElement column = findElement(columnResize, columnToResize);
WebElement moveToPosition = findElement(columnByName, toColumn);
waitUntilClickable(column);
//click on resize
getSupport().clickAndHold(column);
..
...
In PageSupport class:
public void clickAndHold(WebElement onElement) {
Actions actions = new Actions(driver);
actions.clickAndHold(onElement);
}
In Actions class:
public Actions clickAndHold(WebElement onElement) {
action.addAction(new ClickAndHoldAction(mouse, (Locatable) onElement));
return this;
}
But I get the following error:
java.lang.ClassCastException: eu.ohim.tmdsview.selenium.util.WebElementProxy cannot be cast to org.openqa.selenium.internal.Locatable
Apparently it does not like the cast to Locatable, any ideas? Thanks in advance!
UPDATE- WebElementProxy class
package eu.ohim.tmdsview.selenium.util;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.WrapsElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.ohim.tmdsview.test.TestEnv;
/**
* <class>WebElementProxy</class>. Defines all the interaction manners that the framework
* may perform with the application.
* @implements WebElement, WrapsElement
*
*/
public class WebElementProxy implements WebElement, WrapsElement {
private final Logger logger = LoggerFactory.getLogger(getClass());
private By locator;
private WebDriver searchContext;
private WebElement element;
/**
* WebElementProxy. Overloaded constructor.
* @param searchContext. Webdriver search content
* @param locator. By element
*/
public WebElementProxy(WebDriver searchContext, By locator) {
this.locator = locator;
this.searchContext = searchContext;
}
/**
* WebElementProxy. Overloaded constructor.
* @param searchContext. Webdriver search content
* @param element. Webelement object
*/
public WebElementProxy(WebDriver searchContext, WebElement element) {
this.element = element;
this.searchContext = searchContext;
}
/**
* <method>getElement</method> method: recovers to the framework the WebElement selected to use it
* @return the WebElement selected
*/
private WebElement getElement() {
//If the element is not null, the framework returns it
if(element != null) {
return element;
} else {
//If the elements has not found, the framework prints the problematic locator
logger.trace("findElement() [{}]", locator);
return searchContext.findElement(locator);
}
}
/**
* <method>click</method> method: search the element and does click on it
*/
public void click() {
infoWithScrenshot("click() [{}]", locator);
getElement().click();
}
/**
* <method>submit</method> method: search the element and submits it
*/
public void submit() {
infoWithScrenshot("submit() [{}]", locator);
getElement().submit();
}
/**
* <method>sendKeys</method> method: search the element and writes the text passed as a parameter
* @param CharSequence... keysToSend. Text to writes on the application
*/
public void sendKeys(CharSequence... keysToSend) {
infoWithScrenshot("sendKeys() [{}] to element [{}]", keysToSend, locator);
getElement().sendKeys(keysToSend);
}
/**
* <method>clear</method> method: search the element and clears it
*/
public void clear() {
infoWithScrenshot("clear() [{}]", locator);
getElement().clear();
}
/**
* <method>getTagName</method> method: search the element and catch the name of it
* @return the name of the tag as a string
*/
public String getTagName() {
logger.info("getTagName() [{}]", locator);
return getElement().getTagName();
}
/**
* <method>getAttribute</method> method: search the element and catch the attribute of it
* @return the text inside the attribute element
*/
public String getAttribute(String name) {
logger.info("getAttribute() [{}] from [{}]", name, locator);
return getElement().getAttribute(name);
}
/**
* <method>isSelected</method> method: search the element and selects it
* @return a boolean value to know if is selected or not an element
*/
public boolean isSelected() {
logger.info("isSelected() [{}]", locator);
return getElement().isSelected();
}
/**
* <method>isEnabled</method> method: search the element and checks if is enabled it or not
* @return a boolean value to know if is enabled or not an element
*/
public boolean isEnabled() {
logger.info("isEnabled() [{}]", locator);
return getElement().isEnabled();
}
/**
* <method>isEnabled</method> method: search the element and recovers the inside it
* @return a string value with the text inside the element
*/
public String getText() {
logger.info("getText() [{}]", locator);
return getElement().getText();
}
/**
* <method>findElements</method> method: search elements in the web code and returns them
* @param by. By
* @return The list of elements that have found, to use it
*/
public List<WebElement> findElements(By by) {
logger.trace("findElements() [{}] on element [{}]", by, locator);
return getElement().findElements(by);
}
/**
* <method>findElement</method> method: search an element in the web code and returns it
* @param by. By
* @return The element found to use it
*/
public WebElement findElement(By by) {
logger.trace("findElement() [{}] on element [{}]", by, locator);
return getElement().findElement(by);
}
/**
* <method>isDisplayed</method> method: search an element in the web code and check if is displayed
* currently on the "ghost" window browser
* @return Boolean value to know if the element is displayed currently or not
*/
public boolean isDisplayed() {
logger.trace("isDisplayed() [{}]", locator);
return getElement().isDisplayed();
}
/**
* <method>getLocation</method> method: search an element in the web code and gets his location
* @return the Point where the element is
*/
public Point getLocation() {
logger.info("getLocation() [{}]", locator);
return getElement().getLocation();
}
/**
* <method>getSize</method> method: search an element in the web code and gets his size
* @return the element Size of the element
*/
public Dimension getSize() {
logger.info("getSize() [{}]", locator);
return getElement().getSize();
}
/**
* <method>getCssValue</method> method: search an element in the web code and gets his Css value
* @return a string with the property name
*/
public String getCssValue(String propertyName) {
logger.info("getCssValue() [{}] on element [{}]", propertyName, locator);
return getElement().getCssValue(propertyName);
}
/**
* <method>getLocator</method> method: search an element in the web code and gets his localization
* @return the element localization
*/
public By getLocator() {
return locator;
}
/**
* <method>toString</method> method: makes as a string properly formated the WebElementProxy
* @return String properly formated
*/
@Override
public String toString() {
return ConstantsFramework.WEB_ELEMENT_PROXY_LOCATOR + locator + ConstantsFramework.ELEMENT + element + ConstantsFramework.CLOSE_BRACKET;
}
/**
* <method>getWrappedElement</method> method: to obtain the wrappered element
* @return the WebElement to wrapper
*/
@Override
public WebElement getWrappedElement() {
return getElement();
}
/**
* <method>infoWithScrenshot</method> method: adds information to the screenshot taken
* @param msg. String with a comment
* @param Object... args. The screenshot
*/
private void infoWithScrenshot(String msg, Object... args) {
if(TestEnv.instance().isAutomaticScreenshot()) {
String imgPath = ScreenshotUtil.takeScreenshot();
//If the image path exists
if(imgPath != null) {
msg += " [<a class=\"screenshot\" href=\"" + imgPath + "\" target=\"_blank\">before</a>]";
}
}
logger.info(msg, args);
}
}
UPDATE This is what I am trying to resize, click on: style="cursor: col-resize;
<thead>
<th id="grid_ipvalue" class="ui-state-default ui-th-column ui-th-ltr" title="Indication of the product" role="columnheader" style="width: 568px;">
<th id="grid_ApplicantName" class="ui-state-default ui-th-column ui-th-ltr" title="Owner name" role="columnheader" style="width: 426px;" aria-selected="true">
<span class="ui-jqgrid-resize ui-jqgrid-resize-ltr" style="cursor: col-resize;"/>
<div id="jqgh_grid_ApplicantName" class="ui-jqgrid-sortable">
</th>
<th id="grid_did" class="ui-state-default ui-th-column ui-th-ltr" title="Design number" role="columnheader" style="width: 311px;">
Here you can see the table
回答1:
From the html code you've posted it looks like you are using jqGrid
.
So i've try to do the same in the jqGrid demo site. It worked fine for me withour any error.
So try the same with simple code instead of factory model and then check
ChromeDriver version 2.15
Selenium 2.46
Sample code
System.setProperty("webdriver.chrome.driver", "D:\\Madhan\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.get("http://www.trirand.com/blog/jqgrid/jqgrid.html");
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"1\"]/td[2]/span")));
//Click on the Loadingdata menu
WebElement load = driver.findElement(By.xpath("//*[@id=\"1\"]/td[2]/span"));
load.click();
//Click on the JSON Data sub menu
WebElement json = driver.findElement(By.xpath("//*[@id=\"3\"]/td[2]/span"));
json.click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#list2_amount > span")));
//Amount column dragger span
WebElement amountcolumn = driver.findElement(By.cssSelector("#list2_amount > span"));
//total column dragger span
WebElement totalcolumn = driver.findElement(By.cssSelector("#list2_total > span"));
Actions action = new Actions(driver);
//click and hold the amount dragger to total dragger to reize the column
action.clickAndHold(amountcolumn).moveToElement(totalcolumn).release().build().perform();
driver.quit();
来源:https://stackoverflow.com/questions/30984103/cannot-cast-webelement-to-org-openqa-selenium-internal-locatable