Selenium WD |如何在使用循环时从主类调用WebElement列表

时间:2017-12-06 20:29:55

标签: java loops selenium-webdriver elements

我无法在我的主类中声明WebElement列表,然后出于某种原因在另一个类中使用它。

问题:我在Main类中有一个循环,因为页面被重新加载所以元素会被反复识别。 非主要类用于第一轮循环的WebElement列表...但不是第二次,我接着

  

'NoSuchElementException异常'

其他信息:

  1. 我总共有4个课程,但为了消除过度信息的混淆,我只会复制/粘贴主课程 + 调用WebElement列表类
  2. 请注意列表< WebElement> list2 ,这就是我遇到的麻烦 - 再次,只在循环的第二次运行中,一旦它再次尝试使用它 - 我得到上述异常
  3. 循环用于在我导航后继续识别元素。 <页面外
  4. '主'类

    public class SocketMain {
    
    public static void main(String[] args) throws InterruptedException {
    
    
    
        System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe"); // where the chrome drive is
        WebDriver driver = new ChromeDriver(); // setting an object that called "ChromeDriver" | Open the browser without doing anything
    
        boolean statusOfTest=true;
        WebElement Status = null;
        statusOfTest = false;
    
        SocketNotMain.NavigateToMarkets(driver);
    
    
        int size = 1;
        for (int i = 0 ; i < size ; ++i) {
    
        List <WebElement> list2 = driver.findElements(By.cssSelector("[nowrap='nowrap']>a"));
    
        NavigateToInstrumentsInMarkets.EnterInstrumentViaMarkets (driver,statusOfTest, size, i,list2 );
        TestClockOfInstrument.runTestClock(driver, Status);
        }
    
        System.out.println("Status of the test is" +statusOfTest);
    
    }
    

    }

    '调用WebElement列表'类

       package PracticeClasses;
    
    import java.util.List;
    import java.util.NoSuchElementException;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    
    public class NavigateToInstrumentsInMarkets {
    
    public static boolean EnterInstrumentViaMarkets (WebDriver driver,boolean statusOfTest,int size, int i, List<WebElement>list2) throws InterruptedException
    {
    
    
    
            try {
    
                size = list2.size();
                Thread.sleep(3000);
                list2.get(i).click();
                Thread.sleep(2000);
                statusOfTest = true;
    
            }
    
            catch (NoSuchElementException e)
            {
                System.out.println("Page of instrument not found, Page not found error 404 ");
                statusOfTest = false;
            }
    
    
    
    
            // Print insturment's name
    
            try {
                WebElement instrumentName = driver.findElement(By.cssSelector("[class='float_lang_base_1 relativeAttr']"));
                System.out.println(instrumentName.getText());
            }
    
    
    
            catch (NoSuchElementException e)
            {
                System.out.println("Cannot find instrument's name");
                statusOfTest = false;
            }
    
    
    
    
        return statusOfTest;
    }
    

    }

0 个答案:

没有答案
相关问题