我是PhantomJSDriver的新用户,并尝试输入我的电子邮件,然后点击https://www.youtube.com/content_id中的“下一步”按钮进入下一个屏幕。
我可以输入我的电子邮件但是,点击事件不起作用。
我的代码:
DesiredCapabilities caps = DesiredCapabilities.firefox();
caps.setBrowserName("firefox");
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability("handlesAlerts", true);
caps.setCapability("elementScrollBehavior", 1);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.out.println(caps);
WebDriver driver = new PhantomJSDriver(caps);
driver.manage().window().setSize(new Dimension(1920, 1080));
driver.get("https://www.youtube.com/content_id");
System.out.println(driver.getTitle());
WebElement elementEmail = driver.findElement(By.id("identifierId"));
System.out.println(elementEmail);
elementEmail.sendKeys("test@gmail.com");
WebElement elementNext = driver.findElement(By.id("identifierNext"));
System.out.println(elementNext + " :: " + elementNext.getText() + " :: " + elementNext.getAttribute("role"));
elementNext.click();
我也尝试使用下面的代码提交表单,但它不起作用:
WebElement elementForm = driver.findElement(By.tagName("form"));
System.out.println(elementForm + " :: " + elementForm.getText() + " :: " + elementForm.getAttribute("class"));
elementForm.submit();
我也尝试过使用JavascriptExecutor -
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", elementNext);
甚至,尝试使用selenium驱动程序 -
Actions action = new Actions(driver);
action.moveToElement(elementNext).click().perform();
在等待几秒钟后,我尝试在下一个屏幕上显示密码字段时截取屏幕截图,但截取的屏幕截图是初始页面。
try {
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.cssSelector("#password > div.aCsJod.oJeWuf > div > div.Xb9hP > input")).isDisplayed();
}
});
System.out.println("We are on password screen.");
} catch (Exception e) {
System.out.println(e.getMessage());
}
takeScreenshot(driver, "final.png"); // private method which captures screenshot to a file
当我在PhantomJS中运行它时,它工作正常 - 但是,需要运行java。
非常感谢任何帮助。
答案 0 :(得分:0)
我能够通过设置如下所述的用户代理来解决它(只是提出答案,因为它可能对某人有帮助):
String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36";
DesiredCapabilities caps = DesiredCapabilities.firefox();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent", userAgent);