java - 使用 Selenium WebDriver java 单击动态加载页面的元素

标签 java javascript selenium selenium-webdriver

我需要点击动态加载页面的特定元素。滚动页面时生成的 Web 元素。它类似于 jabong 网页。

我尝试在 jabong 网页上这样做,这是我的代码

    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.navigate().to("http://www.jabong.com/men/clothing/"
            + "?source=topnav");


    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    System.out.println("Close the modal popup");
    driver.findElement(By.id("jab-vchr-cls")).click();
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

      /**
       * while(true) loop is required to search the 
       * element until element found.We put find
       * element within try-catch and if it get 
       * exception it scroll the page and again 
       * try to find the element.
       */
    while(true) {

            ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,100)", "");
        try {
            WebElement element = driver.findElement(By.xpath("//*[@id='http:    //static3.jassets.com/p/The-Indian-Garage-Co.-Checks-Red-Casual-Shirt-2889-679124-1-catalog.jpg']/img"));
            Wait<WebDriver> wait_element=new WebDriverWait(driver, 10);
            wait_element.until(ExpectedConditions.elementToBeClickable(element));
            element.click();
            System.out.println("!!!!!!!!!!!!!!At Last Get Success!!!!!!!!!!!!!!!!");
            break;

        }
        catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage());
        }
        }

}

我的问题是

1.有没有更好的方法来做这件事?

2.如何使这个脚本更快?

最佳答案

如果你想避免 while(true) 你可以这样做,虽然我不认为这个循环有任何问题。

    boolean reachedbottom = Boolean.parseBoolean(js.executeScript("return $(document).height() == ($(window).height() + $(window).scrollTop());").toString());

    while (!reachedbottom) {
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,600)", "");
        try {
            reachedbottom=Boolean.parseBoolean(js.executeScript("return $(document).height() == ($(window).height() + $(window).scrollTop());").toString());
            WebElement element = driver.findElement(By.xpath("//*[@id='http://static3.jassets.com/p/The-Indian-Garage-Co.-Checks-Red-Casual-Shirt-2889-679124-1-catalog.jpg']/img"));
            Wait<WebDriver> wait_element = new WebDriverWait(driver, 5);
            wait_element.until(ExpectedConditions.elementToBeClickable(element));
            element.click();
            System.out.println("!!!!!!!!!!!!!!At Last Get Success!!!!!!!!!!!!!!!!");
            break;
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage());
        }
    }

关于java - 使用 Selenium WebDriver java 单击动态加载页面的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20955226/

相关文章:

javascript - 在静态站点的 URL 中使用参数

java - SelendroidDriver 不在 jar 文件中

ruby - 如果找不到元素,则继续执行脚本

java - 在java应用程序中从iPad下载数据

c# - 为什么 Java on Server 和 C# on Client 是流行的选择?

java - 使用位运算符比较两个整数

javascript - 延迟加载 Javascript 以加速我的 Joomla

java - 在 Eclipse Debug模式下复制对象的整个树

javascript - 如何在appData中写入本地文件夹的完整路径

php - "When I fill the following fields"输入今天的日期?