Wild Goat
Wild Goat

Reputation: 3579

Capture HTML after element has been changed

I am looking for automated way to capture HTML after I do some actions on the web page.

For example I select some item in dropdown and HTML has been changed, i want to capture that HTML and dump into file. As result, I will end up with many different HTML files on my hdd.

I was thinking it might be possible to achieve that by using Selenium, maybe some other plugin which would give me possibility so save HTML in automatic manner to file.

Upvotes: 4

Views: 827

Answers (3)

Wild Goat
Wild Goat

Reputation: 3579

I found answer for my own question.

  1. Start a selenium chrome driver server.
  2. Use selenium client connect to and all changes could be captured by using code example below:

Code:

WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), DesiredCapabilities.chrome());
        driver.get("http://google.com");
    By by = new By.ByTagName("div"); 
    List<WebElement> oldDivs = driver.findElements(by);

    while(true){
        try {
            List<WebElement> newDivs = driver.findElements(by);
            if (oldDivs != newDivs) {
                for (WebElement element : newDivs) {
                    String a = element.getAttribute("a");
                    String b = element.getAttribute("b");

                    System.out.println(a + " :" + b);
                }
            }
        }catch (Exception e){
            System.err.println(e);
        }
    }

Upvotes: 1

JeffC
JeffC

Reputation: 25621

I think what you are asking to do will not be that easy. There are other questions on SO (e.g. this one) asking the same thing and there aren't really any good answers. I tried googling for a few mins to find a way to do this. I would think something more like a browser plug-in would exist that would do this for you.

If I were forced to code this using Selenium I would do something like the following...

Create a script that launches the browser and navigates to the page you want to track. In a user-defined interval, the script would grab the page source and compare it to the last capture. If the source is not the same, it would diff the two pages and write the diff to disk. I'm sure there are a number of diffing libraries you could find and use.

The problems with this approach...

  1. If you made too many changes within the defined interval, you would get a glob of changes and not be able to differentiate what action made what change.
  2. If you make the interval too small, you may run into perf issues.
  3. Probably the most significant issue is going to be the fact that you run several tests and then go back and look at the diffs... but you won't have any way to tell what changes correspond to what action since you can't tie the two together other than order of occurrence.

What might be cool is if you could inject a button into the page that when clicked would popup an input dialog that you could type some text into and use that as a label for the upcoming action diff. For example, you click the button and type "choose price" - OK. Now you select a price from a dropdown. The next time you click the button, the script detects the button click and does a quick diff and writes it to disk using the "choose price" label... or something like that.

Upvotes: 1

Mahsum Akbas
Mahsum Akbas

Reputation: 1583

do you mean source code?

for Python:

driver.page_source

for Java:

driver.getPageSource();

you can run these code after each step where page is changed

Upvotes: 2

Related Questions