Reputation: 68
I have written code using selenium web driver java to search for school and to get the first url from the search . my code is working fine without PhantomJS. But as i want to use headless browser, trying the below code to make changes.
WebElement element = driver.findElement(By.name("q"));
PhantomJS could not find this element "driver.findElement(By.name("q"));
" in the below code, i tried with id too, not working out.
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities)caps).setCapability("takesScreenshot", true);
((DesiredCapabilities) caps).setCapability( PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:/Softwares/phantomjs-2.5.0-beta-windows/phantomjs-2.5.0.beta-windows/bin/phantomjs.exe");
// Initialize browser
WebDriver driver = new PhantomJSDriver(caps);
// And now use this to visit Google
driver.get("https://www.google.com");
// Maximize browser
driver.manage().window().maximize();
// find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
String searchSchool = "Bay High School"
// enter something to search for
element.sendKeys(searchSchool);
System.out.println("school name is "+ searchSchool);
// now submit the form
element.submit();
Thread.sleep(500);
WebElement click = driver.findElement(By.name("btnG"));
click.click();
Thread.sleep(2000);
List<WebElement> listings = driver.findElements(By.tagName("div").xpath("//cite[@class='_Rm']"));
listings.size();
Upvotes: 0
Views: 297