Results 1 to 7 of 7
  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default WebEngine find text

    How can I find text on the webengine?
    For example if I go to google.com, how can I get the text from a part of a page? For example how can I have a look what text is in the <title> tags. I know I can do getTitle, but how can I check text in different elements in the page?

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Get html source code from HTMLDocument

    I am using JavaFX's web browser engine.
    I need to get the HTML's page source code from org.w3c.dom.html.HTMLDocument. I have been trying to do this for 2 days and I can't figure out how to do it.

    How can I do this?



    I have posted a similar thread on JavaFX section as well but I got no answers. So I thought I will post this on here. :P

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Get html source code from HTMLDocument

    If you used a URLConnection, you would just read it from the site.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Get html source code from HTMLDocument

    I'm not using URLConnection.
    I'm using JavaFX for the web browser, because I don't really know how to make a web browsers. I have tried to make one but I failed. I know that it would be possible to get the source code with URLConnection but what if you need to be logged in ? For example I would like to find out how many private message I have received using the URLConnection, if I will try it - it will say that I will need to login to see the private messages.
    But in JavaFX there is a web engine and you can get the page's document and other elements of the page. But I just cant figure out how to get the whole source code.



    Another question: would it be hard to make a web browser in java (without Java FX)?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: WebEngine find text

    Lets keep this all in one place. I've merged the two threads.

    I don't know of any way to get the raw HTML from WebEngine, but as you've been told you can use URLConnection. Use the right tool for the job and all that.

    You can get the page text by using
    Java FX Code:
    webEngine.getDocument().getDocumentElement().getTextContent()
    db

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: WebEngine find text

    I've used webEngine.getDocument().getDocumentElement() to get the elements of my page. :P

    But I got an another problem. I don't know whether or not I should make a new thread.

    But I have a thread to check some things and then load a new page.
    I have used this for my loading: Main.Browser.getBrowser().getEngine().load(Constan ts.PageURLS.RACE.getURL());
    The URL is http://lt1.website.lt/race.php
    And the URL is a String.

    But when it tries to load the page I get this error:
    Java FX Code:
    Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
    	at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:218)
    	at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:373)
    	at javafx.scene.web.WebEngine.checkThread(WebEngine.java:581)
    	at javafx.scene.web.WebEngine.load(WebEngine.java:385)
    	at org.srbot.bot.Loop.startLoop(Loop.java:71)
    	at org.srbot.bot.Loop.run(Loop.java:26)
    	at java.lang.Thread.run(Thread.java:662)
    What am I doing wrong?

    Here is my thread:
    Java FX Code:
    while (isRunning) {
    
                System.out.println("Loop started");
                if (!m.isRacing() && !Methods.getURL().contains(BotConstants.PageURLS.RACE.getURL())) {
                    Methods.setStatus("Loading " + BotConstants.PageURLS.RACE.getURL());
    
    
                    Methods.println("Loading page...");
                    Main.Browser.getBrowser().getEngine().load(BotConstants.PageURLS.RACE.getURL());
                    Methods.println("Done loading page");
    
                } else if (Methods.getURL().contains(BotConstants.PageURLS.RACE.getURL())) {
                    Methods.setStatus("Starting race... " + BotConstants.PageURLS.RACE.getURL());
                } else {
                    Methods.setStatus("Waiting for race to be finished.");
                }
    
    
            }
    Last edited by PhQ; 09-15-2011 at 06:02 PM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: WebEngine find text

    What am I doing wrong?
    The error message tells you that.
    Not on FX application thread
    Most FX node methods have to be run on the application thread. If the thread of execution is a background Thread, you will need to wrap the FX node code in a Paltform.runLater(...)

    db

Similar Threads

  1. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  2. Searching through folder to find text inside all files
    By dazzabiggs in forum New To Java
    Replies: 3
    Last Post: 05-03-2011, 01:20 PM
  3. How to find specific text from .txt file
    By krechlich in forum New To Java
    Replies: 11
    Last Post: 03-18-2011, 06:57 AM
  4. Find and Replace in Text File
    By hamidsharifi in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 02:01 AM
  5. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •