Selenium Java脚本滚动无法正常工作

时间:2019-05-14 19:09:47

标签: javascript selenium

我有一个硒javascript代码,可以滚动到表格中的特定Web元素,以便可以选择表格行中该元素旁边的复选框。 我有以下代码有时可以工作,但有时却无法滚动到表中的元素。

public String xapthbuyTrade = "//div[text()='gtiBuyref']";

public void scrollAndSelectTrade(String gtiBuyref) throws InterruptedException {
       // WebDriverWait wait = new WebDriverWait(driver, 10);
       driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        Thread.sleep(300000);
       // wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xapthStatusCategarory)));
        // visibilityOfXpathElementLocated(xapthStatusCategarory);
        ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", driver.findElement(By.xpath(xapthbuyTrade.replace("gtiBuyref", gtiBuyref))));

        driver.findElement(By.xpath(xapthCheckBoxTrade.replace("gtiBuyref", gtiBuyref))).click();
        LOGGER.info("Scroll down till the buy trade visisble");
        visibilityOfCssElementLocated(cssIseUpdate);
        Thread.sleep(1000);
        driver.findElement(By.cssSelector(cssIseUpdate)).click();
        Thread.sleep(3000);
        LOGGER.info("Clicked on update button");
    }

如果我手动滚动,就能看到表格中存在的元素。但是当无法滚动时,出现以下异常

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[text()='TSB0C590']"}
Command duration or timeout: 0 milliseconds
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:160)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at pages.VETCSWSearchPage.scrollAndSelectTrade(VETCSWSearchPage.java:271)
    at RepairUI.RepairHelperUI.linkSellBuyInCSWPage(RepairHelperUI.java:380)
    at steps.StepDefsRepairUI.linkSellBuyInCSW(StepDefsRepairUI.java:154)
    at ?.Then link the sell and buy trade in International Settlements page(CosmicTre.feature:42)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[text()='TSB0C590']"}

我尝试添加延迟,如代码部分中突出显示的那样,但这没有帮助。请让我知道是否需要其他信息。 我正在使用firefox是52.8.0(32位),硒版本是

 <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.9.1</version>
 </dependency>

0 个答案:

没有答案
相关问题