Hi I´m fairly new to HtmlUnit, I got a project where I want to fetch some information from a side and till now everything went smooth finding elements via name or id. But I was unable to get the following Paragraph Element.
<iframe id="content_ifr" frameborder="0" src="javascript:""" allowtransparency="true" title=".." style="width: 100%; height: 307px; display: block;">
<!DOCTYPE >
<html>
<head> ... </head>
<body id="tinymce" class="mceContentBody content post-type-coupon wp-editor" contenteditable="true" onload="window.parent.tinyMCE.get('content').onLoad.dispatch();" dir="ltr">
<p>------ Text from the Element i want to get ------- </p>
</body>
</html>
</iframe>
I tried already:
side.getByXPath("//html/body/p");// zero elements
side.getByXpath("//p");// 27 element but wrong.
side.getByXpath("//body");// 1 element but wrong.
side.getByXpath("//html");// 1 element but wrong.
side.getByXpath("//html/body/div[3]/div[3]/div[2]/div/div[4]/form/div/div/div/div[2]/div/div[2]/span/table/tbody/tr[2]/td/iframe"); // Zero elements found
I checked all elements the Code found with this:
List<?> list =gPage.getByXPath("//p");
for(Object x:list){
HtmlElement y=(HtmlElement) x;
if(y.asXml().contains("Keyword")||y.asText().contains("Keyword")){
System.out.println(y.asText());
}
So in conclusion I couldn´t find the Paragraph Element by his Text. Could you help me find the Paragraph element, so that I´m able to read and write from/to it?
//Initialize WebClient
final WebClient webClient= new WebClient(BrowserVersion.FIREFOX_24);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.waitForBackgroundJavaScript(10000);
//Perform a login.
final HtmlPage page = webClient.getPage("");
final HtmlForm form = page.getForms().get(1);
final HtmlTextInput username = form.getInputByName("log");
final HtmlPasswordInput pw = form.getInputByName("pwd");
username.setValueAttribute("");
pw.setValueAttribute("");
@SuppressWarnings("unused")
HtmlPage page2 = (HtmlPage) form.getButtonByName("login").click();
//Get gutscheinPage
HtmlPage gutscheinPage= webClient.getPage("");
//Change Content of Textfield
HtmlPage pageFrame = (HtmlPage) gutscheinPage.getFrames().get(0).getEnclosedPage();
HtmlElement body =pageFrame.getBody();
HtmlParagraph p =(HtmlParagraph) body.getByXPath("//p").get(0);
p.setTextContent(text);
Done: Change the webClient default Browser and wait for Jscript,use getFrames, find the body and use the now easy XPath to provide me with my Paragraph Element.
I really hope someone will find this helpfull for their own work.
Thanks for every answer.
As you can see, it's in an iframe
. I guess you need switch into the frame first.
Here is the documentation which you should have a try.
// untested Java code, please debug and read documentation yourself
final List<FrameWindow> window = page.getFrames();
final HtmlPage pageTwo = (HtmlPage) window.get(0).getEnclosedPage();
// then find TinyMCE's body, which should be treated as a separated HTML page