我有两个不相关的课程
1)第一个,一个Main类,我粘贴在这里作为参考。它很好地识别了Chrome驱动程序。
2)第二个是非主要类,由于某种原因无法识别我的Chrome驱动程序
(?)如果我打开任何不是Main的类,Chrome驱动程序内部无法识别......为什么会这样?
感谢?
有效的主要课程
import org.openqa.selenium.By;
//WebElement Practice
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.List;
import org.openqa.selenium.chrome.ChromeDriver;
public class SocketTestsFINAL2Dublicate {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.investing.com");
driver.navigate().refresh();
driver.findElement(By.cssSelector("[href = '/markets/']")).click();;
// list |
int size = 1;
for (int i = 0 ; i < size ; ++i) {
List <WebElement> list2 = driver.findElements(By.cssSelector("[nowrap='nowrap']>a"));
//Enter the stock page
size = list2.size();
Thread.sleep(3000);
list2.get(i).click();
Thread.sleep(2000);
// Print instrument name
WebElement instrumentName = driver.findElement(By.cssSelector("[class='float_lang_base_1 relativeAttr']"));
System.out.println(instrumentName.getText());
// Check if stock is opened/closed
WebElement Status = null;
if (Status == driver.findElement(By.cssSelector(".inlineblock.redClockBigIcon.middle.isOpenExchBig-1")));
{
System.out.println("Stock is closed");
driver.navigate().back();
}
// Stock is opened
if (Status == driver.findElement(By.cssSelector(".inlineblock.greenClockBigIcon.middle.isOpenExchBig-1014")))
{
WebElement e3 = driver.findElement(By.id("last_last")); // Find Socket Element#1
String newSocket = e3.getText();
Thread.sleep(1);
String printold = newSocket; // Old socket value for print on screen
String lettersOnlynewSocket1 = newSocket.replaceAll("[\\W]", ""); // remove punctuations
double CurrentSocketValue = Double.valueOf(lettersOnlynewSocket1); // !! Parsing from String to double
double OldSocketValue = CurrentSocketValue ;
Thread.sleep(90000);
WebElement e4 = driver.findElement(By.id("last_last")); // Find Socket Element#2
newSocket = e4.getText();
String printNew = newSocket; // New socket value for print on screen
String lettersOnlynewSocket2 = newSocket.replaceAll("[\\W]", ""); // remove punctuations
CurrentSocketValue = Double.valueOf(lettersOnlynewSocket2);
// Status is : CurrentSocket = After waiting socket upated | oldSocket = Before socket update
if (CurrentSocketValue == OldSocketValue ) // If socket is not updated - Print 'Fail'
System.out.println("Socket is not updated");
else
{
System.out.println("Socket is updated succesfully");
}
System.out.println("Current value is:" +printNew +"old value is:" +printold);
driver.navigate().back();
}
}
}
}
但是,非主要类,不起作用
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SocketNotMain {
System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.investing.com");
driver.navigate().refresh();
driver.findElement(By.cssSelector("[href = '/markets/']")).click();;
public boolean isAvailable ()
{
return false;
}
}
答案 0 :(得分:0)
作为@Grasshopper上面评论的序列你的非主要课程应该是这样的。
public class SocketNotMain {
@Test
public void testSomething() {
System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.investing.com");
driver.navigate().refresh();
driver.findElement(By.cssSelector("[href = '/markets/']")).click();
}
}