Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
[Selenium]如何在web元素上调用重写的方法?_Selenium_Selenium Webdriver - Fatal编程技术网

[Selenium]如何在web元素上调用重写的方法?

[Selenium]如何在web元素上调用重写的方法?,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我在BaseElement类中创建了一个重写方法(click()method),它从WebElement接口实现 如果适用,我想在所有web元素上调用此重写方法 WebElement ele=driver.findElement(By.id(“按钮”)//默认情况下返回webelement类型 如何在上述web元素上调用覆盖click()方法 我尝试过将ele向下转换为BaseElement,如下所示: BaseElement m_ele=(BaseElement)ele//无编译错误,但引发运

我在BaseElement类中创建了一个重写方法(
click()
method),它从WebElement接口实现

如果适用,我想在所有web元素上调用此重写方法

WebElement ele=driver.findElement(By.id(“按钮”)//默认情况下返回webelement类型

如何在上述web元素上调用覆盖
click()
方法

我尝试过将ele向下转换为BaseElement,如下所示:

BaseElement m_ele=(BaseElement)ele//无编译错误,但引发运行时类转换异常

有什么想法可以帮我在WebElement上调用Overrided
click()
方法吗?我不想使用WebElement的内置
click()
方法

如果我需要提供任何其他信息,请告诉我

以下是我的代码片段:

public class BaseElement implements WebElement {

public BaseElement(WebElement element) {
        m_element = element;
}

@Override
    public void click() {
        try {
            m_element.click();
        } catch (WebDriverException e) {
            try {
                logger.debug("Click Failed, Attempting to click by scrolling into view with bottom align option");
                scrollIntoView();
                m_element.click();
            } catch (WebDriverException ef) {
                try {
                    logger.debug("Click Failed, Attempting to click by scrolling into view with top align option");
                    scrollIntoView(true);
                    m_element.click();
                } catch (WebDriverException e2) {
                    try {
                        logger.debug("Click Failed, Attempting to click by scrolling down by 200 pixels");
                        m_jsDriver.executeScript("window.scrollBy(0,200)");
                        m_element.click();
                    } catch (WebDriverException e3) {
                        logger.debug("Click Failed, Attempting to click by scrolling up by 200 pixels");
                        m_jsDriver.executeScript("window.scrollBy(0,-200)");
                        m_element.click();
                    }
                }
            }
        }
    }
}

您可以尝试使用下面的代码来使用Overriden click方法

WebElement ele = driver.findElement(By.id("button")); 
BaseElement m_ele = new BaseElement(ele);
m_ele.click();

用BaseElement类的相关片段更新问题可能重复