有没有什么方法可以获得一个DOM元素并对其进行操作,这样我就可以输入用户名和密码,以便在启动应用程序时自动登录?现在,我已经使用机器人让计算机传递我的登录信息并自动登录,但我需要一个更好的方法。非常感谢任何帮助!
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.net.URL;
public class Main
{
private static String OS = null;
private static Integer CTRL = null;
private static String username = "u1240430";
private static String password = "123123";
public static void main(String[] args) {
OS = System.getProperty("os.name");
if( OS.equalsIgnoreCase("MAC OS X") ){
CTRL = KeyEvent.VK_META;
}else{
CTRL = KeyEvent.VK_CONTROL;
}
try {
Desktop.getDesktop().browse(new URL("https://awebsite.something/login.php").toURI());
startWithLogin();
} catch (Exception e){
System.out.println(e.getMessage());
}
}
private static void startWithLogin() {
try {
Robot r = new Robot();
StringSelection selection = new StringSelection(username);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, null);
r.delay(1500);
r.keyPress(CTRL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_V);
r.keyRelease(CTRL);
r.delay(100);
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
r.delay(100);
selection = new StringSelection(password);
clipboard.setContents(selection, null);
r.delay(100);
r.keyPress(CTRL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_V);
r.keyRelease(CTRL);
r.delay(100);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException ex) {
System.out.println(ex.getMessage());
}
}
}