Нажмите на HTML текст с помощью JAVA - PullRequest
0 голосов
/ 24 сентября 2018

Я пытаюсь создать класс, который может нажать на текст / кнопку в HTML.На странице есть имя пользователя и пароль, поэтому я использовал JSOUP для разбора документа, используя .data для вставки имени пользователя и пароля:

Document document = Jsoup.connect("url").data("user", user)
                .data("password", password)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0")
                .timeout(3000).post();

На данный момент все работает нормально.Но теперь у меня есть кнопка в html, подобная этой:

<a type="button" id="request" onclick="request(this)" class="lightButton"><i class="loading" style="display:none"><span class="ifont"></span></i>Request</a> 

Но я не знаю, как нажать на этот текст / кнопку.

Кто-нибудь знает, как мне это сделать?Спасибо

1 Ответ

0 голосов
/ 24 сентября 2018

это сработало для меня

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestMain {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\tests\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("window-size=1024,768");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new ChromeDriver(capabilities);

        driver.get("http://google.com/");

        if (driver instanceof JavascriptExecutor) {
            ((JavascriptExecutor) driver).executeScript("document.getElementsByClassName(\"gb_we\")[0].click()");
        }

    WebElement fName=driver.findElement(By.id("identifierId"));
    fName.sendKeys("test@gmail.com");

    WebElement btnNext =driver.findElement(By.id("identifierNext"));
    btnNext.click();

    }
}
...