我无法在我的主类中声明WebElement列表,然后出于某种原因在另一个类中使用它。
问题:我在Main类中有一个循环,因为页面被重新加载所以元素会被反复识别。 非主要类用于第一轮循环的WebElement列表...但不是第二次,我接着
'NoSuchElementException异常'
其他信息:
'主'类
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;
}
}