public class LaunchBrowser_TestNG {
@Test
public void LaunchBrowser() throws InterruptedException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs");
WebDriver driver = new PhantomJSDriver(caps);
System.out.println("PhantomJS Headless Driver launched");
// Write your script here
driver.get("https://google.com");
Thread.sleep(5000);
System.out.println ("Launch Browser is successful");
System.out.println("Page Title : " + driver.getTitle());
//Searching for "Fresco Play" in Google search
driver.findElement(By.xpath("//input[@class='gLFyf gsfi']")).sendKeys("Fresco Play");
driver.findElement(By.xpath("//input[@class='gLFyf gsfi']")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
System.out.println("Page Title : " + driver.getTitle());
}
}
By.xpath有一些错误。上面写着-
方法xpath对于方法BY是未定义的。
可能缺少必需的导入。您需要添加:
import org.openqa.selenium.By;
首先,您需要在测试的顶部导入import org.openqa.selenium.By
。然后喜欢:
driver.findElement(By.xpath,value= "//input[@class='gLFyf gsfi']")).sendKeys("Fresco Play");
如果以上代码不起作用,请尝试此操作:
driver.find_element(By.XPATH,"//input[@class='gLFyf gsfi']")