forked from BroManDudeGuyPhD/SirBroBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newjsf.xhtml
68 lines (49 loc) · 2.18 KB
/
newjsf.xhtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bromandudeguyphd.htmlparsing;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.util.Scanner;
public class HTMLUnit {
public static void main(String[] args){
}
public static String submittingForm(String url) throws Exception {
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true);
// Get the first page
HtmlPage page1 = webClient.getPage("https://www.captionbot.ai/");
final HtmlElement button = page1.getFirstByXPath("//*[@id=\"idTextSubmitButton\"]");
final HtmlTextInput textField = page1.getFirstByXPath("//*[@id=\"idTextField\"]");
textField.setValueAttribute(url);
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
DomElement element = page2.getFirstByXPath("//*[@id=\"captionLabel\"]");
if(element.asText().isEmpty() ){
return "Something went wrong";
}
else{
return element.asText();
}
}
}