JxBrowser How to get value from a html node within java - java

Hello guys I am trying out the jxBrowser component and I am unable to the value of selected html component...
List<DOMElement> paragraphs = divRoot.findElements(By.cssSelector("p"));
for (DOMElement paragraph : paragraphs) {
System.out.println("paragraph.getNodeValue() = " +
paragraph.getNodeValue());
}
I am able to find paragraphs.. But can't get their node's value.. or simply <p>I cant get this value<p/> The code must be okay because its just a pure copy of their own sample code: here
So my question is... What have I done wrong? It seems properly imported.. I am using library version 6.19.1 on a macbook. ( And I even tried it on a windows 10 with same result.. )
Or if there is other java browser solution with similar functions.. What I need is to load a page, get some values out of some divs and then simulate click.

DOMElement.getNodeValue() returns the value of this node, depending on its DOMNodeType. The text you are trying to get is a children node for the node, so you need to get it with the following code paragraph.getChildren().get(0).
So, the final code will look like the following:
for (DOMElement paragraph : paragraphs) {
System.out.println("paragraph.getNodeValue() = " +
paragraph.getChildren().get(0).getNodeValue());
}

Related

How do I iterate over an entire document in OpenOffice/LibreOffice with UNO

I am writing java code to access a document open in Libre Office.
I now need to write some code which iterate over the entire document, hopefully in the same order it is shown in the editor.
I can use this code to iterate over all the normal text:
XComponent writerComponent=xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps);
XTextDocument mxDoc=UnoRuntime.queryInterface(XTextDocument.class, writerComponent);
XText mxDocText=mxDoc.getText();
XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, mxDocText);
XEnumeration xParaEnum = xParaAccess.createEnumeration();
Object element = xParaEnum.nextElement();
while (xParaEnum.hasMoreElements()) {
XEnumerationAccess inlineAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, element);
XEnumeration inline = inlineAccess.createEnumeration();
// And I can then iterate over this inline element and get all the text and formatting.
}
But the problem is that this does not include any chart objects.
I can then use
XDrawPagesSupplier drawSupplier=UnoRuntime.queryInterface(XDrawPagesSupplier.class, writerComponent);
XDrawPages pages=drawSupplier.getDrawPages();
XDrawPage drawPage=UnoRuntime.queryInterface(XDrawPage.class,page);
for(int j=0;j!=drawPage.getCount();j++) {
Object sub=drawPage.getByIndex(j);
XShape subShape=UnoRuntime.queryInterface(XShape.class,sub);
// Now I got my subShape, but how do I know its position, relative to the text.
}
And this gives me all charts (And other figures I guess), but the problem is: How do I find out where these charts are positioned in relation to the text in the model. And how do I get a cursor which represent each chart?
Update:
I am now looking for an anchor for my XShape, but XShape don't have a getAnchor() method.
But If I use
XPropertySet prop=UnoRuntime.queryInterface(XPropertySet.class,shape);
I get the prop class.
And I call prop.getPropertyValue("AnchorType") which gives me an ancher type of TextContentAnchorType.AS_CHARACTER
but I just can't get the anchor itself. There are no anchor or textrange property.
btw: I tried looking into installing "MRI" for libre office, but the only version I could find hav libreoffice 3.3 as supported version, and it would not install on version 7.1
----- Update 2 -----
I managed to make it work. It turns out that my XShape also implements XTextContent (Thank you MRI), so all I had to do was:
XTextContent subContent=UnoRuntime.queryInterface(XTextContent.class,subShape);
XTextRange anchor=subContent.getAnchor();
XTextCursor cursor = anchor.getText().createTextCursorByRange(anchor.getStart());
cursor.goRight((short)50,true);
System.out.println("String=" + cursor.getString());
This gives me a cursor which point to the paragraph, which I can then move forward/backward to find out where the shape is. So this println call will print the 50 chars following the XShape.
How do I find out where these charts are positioned in relation to the text in the model. And how do I get a cursor which represent each chart?
Abridged comments
Anchors pin objects to a specific location. Does the shape have a method getAnchor() or property AnchorType? I would use an introspection tool such as MRI to determine this. Download MRI 1.3.4 from https://github.com/hanya/MRI/releases.
As far as a cursor, maybe it is similar to tables:
oText = oTable.getAnchor().getText()
oCurs = oText.createTextCursor()
Code solution given by OP
XTextContent subContent=UnoRuntime.queryInterface(XTextContent.class,subShape);
XTextRange anchor=subContent.getAnchor();
XTextCursor cursor = anchor.getText().createTextCursorByRange(anchor.getStart());
cursor.goRight((short)50,true);
System.out.println("String=" + cursor.getString());

Parsing xml with multi childs using jsoup

I have an xml file that looks as follows - link.
I would like to get the title from it.
In order to do so, I did the following:
Document bookDoc = Jsoup.connect( url ).parser( Parser.xmlParser() ).get();
Node node = bookDoc.childNode( 2 ).childNode( 3 ).childNode( 3 );
This returns me this:
Now I have 2 questions:
Isnt there any simpler way to get this title instead of using all of these childNodes? My worry is that in some result the title wont exactly be at childNode(3) and all my code wont work.
How do I eventually get this title? Im stuck at this point and cant get the string of the title.
Thank you
You can use selectors to access elements. Here you want to select by tag name. Two ways to get the element you want:
String title1 = bookDoc.select("record>display>title").text();
String title2 = bookDoc.selectFirst("record").selectFirst("display").selectFirst("title").text();
If you want to select more complicated things read:
https://jsoup.org/cookbook/extracting-data/dom-navigation
https://jsoup.org/cookbook/extracting-data/selector-syntax
But you probably won't need them for parsing this XML.

WebElement getText() doesn't work on Winium-Desktop

I'm trying to execute getText() on a WebElement instance but for some reason it doesn't work: pcresultsList.get(0).getText()
Maybe Winium doesn't support this method?
My code is:
public void monitorBasic() throws InterruptedException {
monitorFrame = driver.findElement(By.name("BEAM Monitor"));
WebElement resultsFrame = monitorFrame.findElement(By.id("ReportListBox"));
List<WebElement> pcresultsList = resultsFrame.findElements(By.className("TextBlock"));
System.out.println(pcresultsList.get(0).getText());
}
sorry for not posting the error I'm getting - the site tells me that my code is not indent and when i'm trying to present it as code the site tells i have too much code. :)
Have you tried using:
System.out.println(pcresultsList.get(0).getAttribute("Value"));
Also try to get other properties of the element to confirm you can get info.
The code below works for me:
element = Window.findElement(By.xpath("//*[contains(#ControlType,'ControlType.DataItem') and contains(#Name,'G15')]"));
System.out.println("Cell text: " + element.getText()); //writes Shipments
Let us know if this helps.
As far as I understand you are trying to get some text from an element which is inside a frame. In that case you need to switch to the frame first then acquire the text of any of the element within the frame.
You need to do:
Apply ImplicitlyWait for 2/3 seconds.
driver.switchTo().frame("BEAM Monitor"); // assuming that's the frame name.
Then click on a element inside the frame :
driver.findElement(By.id("ReportListBox")).click();
Create a list to get all the elements in a list:
List<WebElement> pcresultsList = driver.findElements(By.className("TextBlock"));
Create a for loop to iterate through the elements to get the text:
for(WebElement pcresult : pcresultsList)
{
System.out.println(pcresult.getText());
}
Let me know if this helps you.
I've found that with Winium, you need to get the Name attribute because getText() does not work correctly.

XPath- Getting Element from a table having dynamic ID

I'm trying to automate my Test Cases using Selenium for an OBIEE application. Now, I need to read a value from a tabular report generated. The problem is, the ID of the last cell where the total is, keeps on changing.
For example- Currently the id is: db_saw_9270_6_1610_0.
After refreshing, the ID becomes something else. The 4 numbers in between (9270) changes. The remaining bit are the same. I'm using the following logic to capture this element:
driver.findElement(By.xpath(".//*[contains(#id, '_6_1610_0')]")).getText();
But, it is returning org.openqa.selenium.NoSuchElementException: Unable to locate element:
Please tell me where did I go wrong and what should I do?
you can try starts-with and substring (as a substitute for xpath 2.0 methdod ends-with):
string xpath = "//*[starts-with(#id, 'db_saw_') and substring(#id, string-length(#id) - 8) = '_6_1610_0']"
driver.findElement(By.xpath(xpath)).getText();
You can try below xpath:-
driver.findElement(By.xpath("//*[starts-with(#id, 'db_saw')]")).getText();
driver.findElement(By.CSSselector("a[id*='_6_1610_0']")).getText();
Note: the a represents a html element. If your id is in a element then you have to replace a by table.
Check this out for more examples with css selector

Selenium (java) can't get text from nested element without printing it

I try to get text from title of modal pop-up window. On the page there are many such windows - each with uniqe id. In each such modal window all elements have the same class names so first I need to point to the correct window and then look for particular element.
So I do it with this code:
public String getRFRTitle(String rfrNumber) {
return driver.findElement(By.id("rfr-details-dialog-"+rfrNumber)).
findElement(By.className("modal-title")).getText();
}
But it's not displaying for me anything.
What I have found is that when I print this title text before, this function works correctly.
I added this before returning value from the function:
System.out.println("tite: "+ driver.findElement(By.id("rfr-details-dialog-"+rfrNumber)).
findElement(By.className("modal-title")).getText());
I tried with initialisation of variables before returning text but without luck.
I can go with my workaround but I'm curious is this Java or Selenium issue.
This will most likely be a timeout issue. The following might work for you and if it doesn't, the stacktrace will give you more feedback.
By locator = By.cssSelector("#rfr-details-dialog-" + rfrNumber + " .modal-title");
int timeoutInSeconds = 10;
WebElement foundElement = new WebDriverWait(webdriver, timeoutInSeconds).until(ExpectedConditions.visibilityOfElementLocated(locator));
System.out.println("tite: " + foundElement.getText());

Categories

Resources