-1

所以这就是我想要做的,我需要java来自动打开一个像mail.google.com这样的网页,然后点击一个按钮将用户名和密码输入到相应的框中。现在我知道了打开浏览器并将其定位到提供的特定 url 的代码,我只是想知道是否有办法告诉 java 将用户名和密码填写到相应的框中。顺便说一句,我已经完成了使用 JAVA 将 MYSQL 数据库中的数据转换为字符串变量...任何帮助或建议?

4

1 回答 1

1

使用硒。这是一个很好的库,您可以在其中操作网页操作。即 selenium.isElementPresent("这里是元素的 xpath"); 将检查元素是否已加载,如果是,您可以使用 selenium.type("xpath", String/integer) 来输入该字段。然后通过 selenium.click("element"); 您可以单击诸如提交按钮之类的元素

例如(不检查当前元素),应该打开 Google.com 并搜索“键入一些随机文本”:

package com.example.tests;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.regex.Pattern;

public class Googletest {
    private Selenium selenium;

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "https://www.google.com/");
        selenium.start();
    }

    @Test
    public void testGoogletest() throws Exception {
        selenium.open("/");
        selenium.type("//div[@id='gs_lc0']/input", "type some random text");
        selenium.click("//div[@id='gbqfbw']/button");
    }

    @After
    public void tearDown() throws Exception {
        selenium.stop();
    }
}
于 2013-03-28T22:08:27.727 回答