Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
Java 我可以扩展WebElement的.click方法吗?_Java_Selenium - Fatal编程技术网

Java 我可以扩展WebElement的.click方法吗?

Java 我可以扩展WebElement的.click方法吗?,java,selenium,Java,Selenium,有没有办法扩展WebElement的.click方法 我想添加几行代码,以适应一些问题,我们有一个内部网站 我想补充一点: WebDriverWait wait = new WebDriverWait(driver, 5); wait.until(ExpectedConditions.elementToBeClickable(pageElement)); 现在我知道你们中的一些人可能会说使用隐式等待。我已经考虑过了,但是我编写的一些页面需要10-30秒才能加载。一些页面加载速度非常快,但随后显

有没有办法扩展WebElement的.click方法

我想添加几行代码,以适应一些问题,我们有一个内部网站

我想补充一点:

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(pageElement));
现在我知道你们中的一些人可能会说使用隐式等待。我已经考虑过了,但是我编写的一些页面需要10-30秒才能加载。一些页面加载速度非常快,但随后显示的按钮是基于单击其他按钮的条件,我知道按钮应该在5秒内加载。我不想让每个按钮都等上30秒。这可能会发生数百次,我不希望脚本花那么长时间


是否有方法向单击事件添加显式等待?

尝试FluentWait,这将每5秒检查一次元素

Wait wait = new FluentWait(driver).withTimeout(30, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

实际上不需要重写
。单击()
,正如@lauda在评论中所说的,这不是一个好的做法。你不需要添加很多代码来等待元素被点击,所以它将添加数千行代码的说法是不正确的。您只需一行代码就可以轻松完成此任务。。。所以没有额外的代码行。话虽如此。。。您应该少关注它将添加多少行代码,并考虑:

  • 维护起来有多容易
  • 我是否给代码库增加了不必要的复杂性
  • 它会更具可读性吗
  • ……等等
  • 代表性按钮的示例单击方法

    public void clickButtonX()
    {
        new WebDriverWait(driver, timeOutInSeconds).until(ExpectedConditions.elementToBeClickable(buttonXLocator)).click();
    }
    
    在课程的顶部声明了这些内容

    private By buttonXLocator = By.id("buttonXId");
    private int timeOutInSeconds = 10;
    
    话虽如此。。。我认为这不是正确的方法。对于页面对象模型,您应该让每个页面类句柄等待页面完成加载。。。这样,您就不必等待按钮加载后再单击它们。在动态加载按钮的情况下,触发动态加载的操作应等待加载完成

    package sandbox;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import common.Functions;
    
    public class _PageTemplate
    {
        private WebDriver driver;
        private By dynamicPageLoadLocator = By.id("someId");
        private By buttonXLocator = By.id("buttonXId");
        private By dynamicLinkLocator = By.id("dynamicLinkId");
        private By dynamicSectionElementLocator = By.id("dynamicSectionId");
    
        public _PageTemplate(WebDriver webDriver) throws IllegalStateException
        {
            Functions.waitForPageLoad(driver);
    
            // see if we're on the right page
            if (!driver.getCurrentUrl().contains("samplePage.jsp"))
            {
                throw new IllegalStateException("This is not the XXXX Sample page. Current URL: " + driver.getCurrentUrl());
            }
    
            // for dynamic pages, wait for a specific element to signal the dynamic load is complete
            new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(dynamicPageLoadLocator));
        }
    
        public void clickButtonX()
        {
            // no wait needed here
            driver.findElement(buttonXLocator).click();
        }
    
        public void clickDynamicLink()
        {
            // clicking this link triggers part of the page to change, reload, etc. ...
            driver.findElement(dynamicLinkLocator).click();
            // ... so after the click, we wait for the dynamic part of the page to finish by locating an element that is inside the dynamically
            // loaded portion and wait for it to be visible
            new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(dynamicSectionElementLocator));
        }
    }
    
    这是在某个Utils类中声明的

    /**
     * Waits for the browser to signal that the DOM is loaded. For dynamic pages, an extra wait may be necessary.
     */
    public static void waitForPageLoad(WebDriver driver)
    {
        new WebDriverWait(driver, 30).until(new ExpectedCondition<Boolean>()
        {
            public Boolean apply(WebDriver webDriver)
            {
                return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
            }
        });
    }
    
    /**
    *等待浏览器发出加载DOM的信号。对于动态页面,可能需要额外等待。
    */
    公共静态void waitForPageLoad(WebDriver驱动程序)
    {
    新的WebDriverWait(驱动程序,30)。直到(新的ExpectedCondition()
    {
    公共布尔应用(WebDriver WebDriver)
    {
    return((JavascriptExecutor)driver.executeScript(“return document.readyState”).equals(“complete”);
    }
    });
    }
    
    现在有了这个框架,我们不再需要向每个按钮添加代码,以确保在单击之前按钮可用。我建议您对所有代码都采用这种方法。只在需要的地方等待,而不是到处撒播等待

  • 等待页面加载
  • 触发动态页面更改后,等待页面更改完成

  • 如果你只做这两件事,你将只在非常特定的地方等待,而不需要到处等待(减少调试时的复杂性和混乱等)。

    在C中,我只会重写该方法,但我不知道在Java中如何做。为什么不创建一个包装click()的静态实用程序方法呢您不应该自定义一个通用的方法来解决特定的问题,这不是一个好的做法。使用页面对象并在这些特定操作中处理您遇到的问题。@lauda通常我会同意您的意见,但在这种情况下,为每个按钮添加代码(有数百行)将实际添加数千行代码。我创建了一个类,然后将元素传递给它,然后执行我需要执行的操作,但是代码的可读性不如我希望的那样好。@Grasshopper你能提供一个例子吗?