Исключение нулевого указателя при выполнении прокрутки в сценарий просмотра в селене - PullRequest
0 голосов
/ 07 июня 2018

Scroll View

ham.click();
Thread.sleep(1000);
//js.executeScript("window.scrollBy(0,500)");
WebElement we4 = driver.findElement(By.xpath(property.getProperty("Intouch")));
je.executeScript("arguments[0].scrollIntoView(true);", we4);
we4.click();
Thread.sleep(2000);

В приведенном выше коде я хочу прокрутить строку меню гамбургера, но когда я выполняю вышеуказанные строки кода, я обнаружил исключение нулевого указателя.

1 Ответ

0 голосов
/ 22 июня 2018

Замените свою находку

WebElement we4 = driver.findElement(By.xpath(property.getProperty("Intouch")));

на

private WebDriverWait wait_element;

wait_element = new WebDriverWait(driver, 40);

WebElement we4 = wait_element.until(ExpectedConditions.presenceOfElementLocated(By.xpath(property.getProperty("Intouch"))));

Second, Thread.sleep ();плохая практикаЯ несу с собой эти вспомогательные методы от проекта к проекту.Вы можете использовать их.

public void waitForBrowserReadystateComplete(WebDriver webDriver) {
    for (int a=0; a<20; a++) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) webDriver;
        if (javascriptExecutor.executeScript("return document.readyState")
                .toString().equals("complete")) {
            break;
        }
        sleepResponsibly(500);
    }
}

public void sleepResponsibly(int timeMillisecond){
    try{
        Thread.sleep(timeMillisecond);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt(); 
        throw new RuntimeException(ex);
    }
}

/**
 * 
 * @param webDriver
 * @param elementToScrollIntoView
 * @param blockOption - If true, the top of the element will be aligned to the top of the visible area of the scrollable ancestor.
 *                      If false, the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor
 */
public void explicitScrollIntoView(WebDriver webDriver, WebElement elementToScrollIntoView, boolean blockOption) {
    final String scriptStr = "arguments[0].scrollIntoView(" + blockOption + ");";
    ((JavascriptExecutor) webDriver).executeScript(scriptStr, elementToScrollIntoView);

    sleepResponsibly(500);

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...