使用JavaScript scrollIntoView会导致模糊元素

时间:2018-05-04 15:38:39

标签: javascript java html selenium

我正在使用Java,Selenium& amp;测试Web应用程序。 TestNG的。

我正在尝试单击仅由打开弹出窗口的占位符引用的元素。

我必须滚动到元素才能将其显示出来。

这是滚动代码:

public static boolean scrollIntoView(WebElement element, WebDriver driver) {
    debug.print(thisClass + " scroll into view...");
    int attempts = 0;
    while (attempts < 2) {
        try {
            // Create instance of Javascript executor
            JavascriptExecutor je = (JavascriptExecutor) driver;
            // now execute query which actually will scroll until that element has appeared
            je.executeScript("arguments[0].scrollIntoView(true);", element);
            debug.print(thisClass + " scrolled into view!");
            return true;
        } catch (StaleElementReferenceException e) {
            // expected
            break;
        } catch (Exception e) {
            int errorCode = 1520008991;
            System.err.println(thisClass + " Error Code: " + errorCode + " error in scrollIntoView: " + e.getMessage());
        }
        attempts++;
    }
    return false;
}

这是我的代码,以找到&amp;单击元素

// identifier = text used to identify placeholder
public static boolean findXpathPlaceholderAndClick(WebDriver driver, String identifier) {
    printLine();
    myPrint(thisClass + " findXpathPlaceholderAndClick. ");
    myPrint(thisClass + " Find placeholder using identifier: " + identifier);
    List<WebElement> placeholders = driver
            .findElements(By.xpath("//label[contains(@class, 'label-placeholder-text')]"));

    myPrint(thisClass + " placeholders count: " + placeholders.size());
    for (WebElement element : placeholders) {
        // select an element
        if(!element.isDisplayed()) {
            Common.scrollIntoView(element, driver);
        }
        String text = element.getAttribute("innerHTML");
        myPrint(thisClass + " text: " + text);
        if (text.contains(identifier)) {
            myPrint(thisClass + " found " + text);
            String forId = element.getAttribute("for");
            myPrint(thisClass + " id of input field = " + forId);
            WebElement inputField = driver.findElement(By.id(forId));
            if (!inputField.isDisplayed()) {
                return false;
            }
            try {
                inputField.click();
                return true;
            } catch (Exception e) {
                int errorCode = 1523444596;
                System.err.println(thisClass + " error Code: " + errorCode + " Exception: " + e.getMessage());
                e.printStackTrace();
                return false;
            }
        }
    }
    return false;
}

这是我试图点击的实际元素的HTML:

<div class="form-group label-placeholder">
    <input class="form-control text-ellipsis-global" id="gwt-uid-2401" data-empty="false" style="" type="text"> 
    <label class="label-placeholder-text" for="gwt-uid-2401">Geometry Required</label> 
    <label class="label-placeholder-icon" for="gwt-uid-2401" style="display: block;"> <i class="icon-display-list"></i> </label>
</div>

最后,这是我日志的输出:

    ------------------------------------------------------------------------------------------------------------------------------
utils.Common                                         findXpathPlaceholderAndClick. 
utils.Common                                         Find placeholder using identifier: Geometry Required
utils.Common                                         placeholders count: 6
utils.Common                                         text: Design name
utils.Common                                         text: Icon
utils.Common                                         text: Colour
utils.Common                                         text: Reference To
utils.Common                                         scroll into view...
utils.Common                                         scrolled into view!
utils.Common                                         text: Network
utils.Common                                         scroll into view...
utils.Common                                         scrolled into view!
utils.Common                                         text: Geometry Required
utils.Common                                         found Geometry Required
utils.Common                                         id of input field = gwt-uid-2401
tests.designer.CreateAssetDesignTest                 *** click Geometry Required failed ***
tests.designer.CreateAssetDesignTest                 ASSERT:  click Geometry Required?  false

utils.Common                                         Generating screenshot. 
utils.Common                                         error Code: 1523444596 Exception: Element <input id="gwt-uid-2401" class="form-control text-ellipsis-global" type="text"> is not clickable at point (150,85) because another element <button class="gwt-Button form-control btn back-button ripple-container" type="button"> obscures it
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'SSTAPLE', ip: '172.16.190.147', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 59.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 23160, moz:profile: C:\Users\SStaple\AppData\Lo..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 18bdbb72-a68a-49d7-a7d2-9dc00f455546

所以,它找到了元素OK,&amp;试图点击它,但发现那个元素被静态元素遮挡了。

我很确定滚动是个问题。

我有什么想法可以解决这个问题吗?

0 个答案:

没有答案