我正在尝试使用selenium自动发送电子邮件我正在使用幻影js(无头)
我收到以下异常
org.openqa.selenium.remote.UnreachableBrowserException与远程浏览器通信时出错
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
((DesiredCapabilities) caps).setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"C:\\jars\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe"
);
//File file = new File("C:/jars/phantomjs-2.0.0-windows/bin/phantomjs.exe");
// System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver d = new PhantomJSDriver(caps);
//WebDriver d=new HtmlUnitDriver();
WebDriverWait wait = new WebDriverWait(d, 10);
d.get("https://www.gmail.com/intl/en/mail/help/about.html");
System.out.println("navigated to gmail");
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
d.manage().window().setSize(new Dimension(1576, 798));;
d.findElement(By.id("gmail-sign-in")).click();
System.out.println("Clicked on Signin");
Thread.sleep(9000);
d.findElement(By.id("Email")).sendKeys("chaitanyapujari97@gmail.com");
System.out.println("Entered Email");
Thread.sleep(9000);
d.findElement(By.id("next")).click();
System.out.println("Clicked next");
d.findElement(By.id("Passwd")).sendKeys("your pwd");
System.out.println("Entered password");
Thread.sleep(9000);
d.findElement(By.id("signIn")).click();
System.out.println("Clicked on signin");
Thread.sleep(9000);
d.findElement(By.xpath("html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[1]/div/div")).click();
System.out.println("Clicked on Compose email");
//Thread.sleep(9000);
Thread.sleep(9000);
d.findElement(By.name("to")).sendKeys("your email");
System.out.println("Entered To address");
Thread.sleep(9000);
d.findElement(By.name("q")).click();
d.findElement(By.name("subjectbox")).sendKeys("PHANTOm Js");
System.out.println("Entered Subject");
//WebElement webElement=d.findElement(By.name("subjectbox"));
/*String keysPressed = Keys.chord(Keys.CONTROL, Keys.RETURN);
WebElement element=d.findElement(By.xpath("html/body"));
element.sendKeys(keysPressed) ;*/
Thread.sleep(9000);
d.findElement(By.xpath("html/body/div[14]/div/div/div/div[1]/div[3]/div[1]/div[1]/div/div/div/div[3]/div/div/div[4]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div/div/div[4]/table/tbody/tr/td[1]/div/div[2]")).click();
System.out.println("Clicked On send");
我可以添加收件人电子邮件地址和主题无法点击发送按钮。
我无法弄清楚我是Headless Selenium自动化的新手,请帮忙,
答案 0 :(得分:0)
请试试这个,我希望它会对你有所帮助,为什么你要使用这么长的XPATH,它必须从html遍历。
d.findElement(By.xpath(".//tr[@class='n1tfz']/td[1]/div[1]/div[2]")).click();
答案 1 :(得分:0)
我发现问题在于我正在使用jar我正在使用jar phantomjsdriver-1.1.0.jar我已经更改了jar的版本现在是phantomjsdriver-1.2.1.jar工作得很好。
感谢帮助朋友。