Selenium - 启动时出错 Chrome
Selenium - Error Launching Chrome
当我在第 11 行调用 System.setProperty() 时出现此错误
Multiple markers at this line
- Syntax error on token ",", < expected
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error, insert "Identifier (" to complete
MethodHeaderName
- Syntax error, insert "SimpleName" to complete
QualifiedName
- Syntax error on token ".", @ expected after this token
我知道 chromedriver.exe 的路径是正确的,因为我检查过。我认为这只是我 System.setProperty().
的位置
谁能帮我解决这个问题,这样我就不会收到错误消息
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
public class BugLogin {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver chromeDriver = new ChromeDriver();
@BeforeTest
public void setup() throws Exception {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("url");
}
public boolean bug() {
WebElement test = driver.findElement(By.id(""));
while(true){
if(test.getText().equals("text")){
driver.findElement(By.id(""));
driver.findElement(By.id("")).clear();
driver.findElement(By.id("")).sendKeys("text");
driver.findElement(By.id(""));
driver.findElement(By.id("")).clear();
driver.findElement(By.id("")).sendKeys("text");
driver.findElement(By.id("")).click();
break;
}
else{
driver.navigate().back();
return true;
}
}
return false;
}
}
为什么不将设置放在 setup
方法中?
@BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
chromeDriver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("url");
}
问题是由于以下语句引起的:
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
Reason: You cannot use a statement containing executable code outside
the class methods and only declare and initialize an identifier in
definition.
在定义中定义 ChromeDriver 并稍后在@BeforeTest 中初始化它可能会解决您的问题。
WebDriver chromeDriver;
@BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
chromeDriver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("url");
}
当我在第 11 行调用 System.setProperty() 时出现此错误
Multiple markers at this line - Syntax error on token ",", < expected - Syntax error, insert ")" to complete MethodDeclaration - Syntax error, insert "Identifier (" to complete MethodHeaderName - Syntax error, insert "SimpleName" to complete QualifiedName - Syntax error on token ".", @ expected after this token
我知道 chromedriver.exe 的路径是正确的,因为我检查过。我认为这只是我 System.setProperty().
的位置谁能帮我解决这个问题,这样我就不会收到错误消息
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
public class BugLogin {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver chromeDriver = new ChromeDriver();
@BeforeTest
public void setup() throws Exception {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("url");
}
public boolean bug() {
WebElement test = driver.findElement(By.id(""));
while(true){
if(test.getText().equals("text")){
driver.findElement(By.id(""));
driver.findElement(By.id("")).clear();
driver.findElement(By.id("")).sendKeys("text");
driver.findElement(By.id(""));
driver.findElement(By.id("")).clear();
driver.findElement(By.id("")).sendKeys("text");
driver.findElement(By.id("")).click();
break;
}
else{
driver.navigate().back();
return true;
}
}
return false;
}
}
为什么不将设置放在 setup
方法中?
@BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
chromeDriver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("url");
}
问题是由于以下语句引起的:
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
Reason: You cannot use a statement containing executable code outside the class methods and only declare and initialize an identifier in definition.
在定义中定义 ChromeDriver 并稍后在@BeforeTest 中初始化它可能会解决您的问题。
WebDriver chromeDriver;
@BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
chromeDriver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("url");
}