为什么HtmlUnit中的.click()在我的代码中不起作用?

时间:2019-10-25 22:06:46

标签: java web-scraping htmlunit

我正在尝试为inbox.lv创建电子邮件发件人

到目前为止,我已经可以登录,访问邮件撰写页面并编写以下内容:收件人和邮件本身。

但是我的问题是.click()使我回到了以前的页面。我正在使用page.asTxt进行检查。

这是我的代码:

public class main {
    public static void main(String[] args) {
        String loginUrl = "https://www.inbox.lv/";
        String login = "actualemail@inbox.lv";
        String password = "Actualemail" ;

        String reciever = "actualemail@inbox.lv";
        String theme = "testing";
        String message = "Hello i am testing";

        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

        try {
            System.out.println("Starting autoLogin on " + loginUrl);
            WebClient client = autoLogin(loginUrl, login, password);
            sendmail(client,reciever,theme,message);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }



    public static WebClient autoLogin(String loginUrl, String login, String password) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
        WebClient client = new WebClient();
        client.getOptions().setCssEnabled(false);
        client.getOptions().setJavaScriptEnabled(true);
        client.getOptions().setThrowExceptionOnScriptError(false);
        client.setAjaxController(new NicelyResynchronizingAjaxController());

        HtmlPage page = client.getPage(loginUrl);

        HtmlInput inputPassword = page.getFirstByXPath("//input[@id='pass']");
        HtmlInput inputLogin = page.getFirstByXPath("//input[@id='imapuser']");

        inputLogin.setValueAttribute(login);
        inputPassword.setValueAttribute(password);

        HtmlForm loginForm = inputPassword.getEnclosingForm() ;
        page = client.getPage(loginForm.getWebRequest(null));

        return client;
    }

    public static void sendmail(WebClient client, String reciever, String theme, String message) throws IOException {
        HtmlPage page = client.getPage("https://mail.inbox.lv/compose?mailbox=INBOX&page=1");

        HtmlInput inputReciever = page.getFirstByXPath("//input[@id='suggest-to']");
        HtmlInput inputTheme = page.getFirstByXPath("//input[@id='subject']");
        HtmlTextArea inputMessage = page.getFirstByXPath("//textarea[@id='message']");

        inputReciever.setValueAttribute(reciever);
        inputTheme.setValueAttribute(theme);
        inputMessage.setText(message);

        System.out.println(page.asText());
        System.out.println("____________________________________________________________________");


        HtmlButton send = page.getFirstByXPath("//button[@id='toolbar_primary_btn_send']");
        HtmlPage sendPage=send.click();
        client.waitForBackgroundJavaScript(10000);

        try {
            Thread.sleep(5000);
        }catch (Exception e){
            System.out.println("Can't sleep thread");
        }

        System.out.println(sendPage.asText());
    }
}

页面的多次打印是在发送前后检查页面。

代码中的电子邮件和密码是合法的,因此您可以使用它来查看“写电子邮件”页面。

0 个答案:

没有答案