Need to compare 2 html documents using Java & selenium

随声附和 提交于 2019-12-03 22:55:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!