问题
I have to check the differences between two html pages: One is an old version and the other is the updated page.
I need Selenium to get the source of the two pages and then compare them to check the differences.
How can I achieve this using Java & Selenium?
Below is the flow I have:
- Go to the old page using Selenium
- Collect all the data for the old web page
- Convert the data to a standard format (XML probably); create a class to do this
- Save the data to a file
- Go to the new page using Selenium
- Collect all the data for the new web page
- Convert the data to a standard format (XML probably); use the class you created to do this
- Save the data to a file
- Use a Java library to diff the two files
Can anyone give ideas/examples in how to implement this?
回答1:
Use selenium.getHtmlSource() for each page and either compare the returned Strings in Java or save them to files and use a command line tool such as "diff".
If using Java, two methods I can think of are using something like StringUtils.difference() (almost certainly a mess) or better yet Google Diff. Google diff has a Java library and test example.
Of course the outputs of these will be a literal diff, you might want something more custom to make sense of changes in HTML.
来源:https://stackoverflow.com/questions/3242640/need-to-compare-2-html-documents-using-java-selenium