Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate Jsoup with WebDriver?

In my WebDriver project, I have planned to add Jsoup to get 'parent' and 'siblings' and few other features. I need to find an element through Jsoup and click its parent using WebDriver. That means I need to convert a Joup element object to WebElement object. Please let me know how I can do this, if this is feasible.

If it is not possible to integrate Jsoup and WebDriver in such way, then please discuss on how I can get parents and all using WebDriver.
Also, is it possible to list ALL possible elements present under a particular WebElement?

like image 652
Abhishek Mukherjee Avatar asked Apr 08 '14 08:04

Abhishek Mukherjee


Video Answer


1 Answers

It's quite interesting we're doing the similar approach, integrating JSoup and Selenium WebDriver. I can understand your issue especially dealing with some dynamic website based on some Javascript framework which has no stable IDs or attributes.

Our solution looks like the following, and hopefully it could be some advice for you:

  • webDriver.getPageSource() to get the current HTML source
  • use JSoup to parse this HTML source, and leverage Jsoup selector (which is much more powerful than Selenium) to locate the target element
  • get parents or siblings of this element
  • write an iteration function to get element xPath, such as //body/div[2]/form[1]/input[3]
  • webDriver.findElement(By.xpath(...)) to locate element in selenium context

EDITED

The idea of the iteration function is:

  • first check the tag of your parent node, if it is body, then iteration ends
  • if not , then use getSiblings to check the index of the node among all the nodes with same tag, e.g, the 3rd div, then equals to div[3]
  • iterate to your parent node, and do the same procedures

Once you get the xpath of the child node, and parent node, just replace parent node xpath to be empty string inside the child node xpath, finally you can get the relative xpath.

like image 71
SmartekWorks Avatar answered Oct 10 '22 04:10

SmartekWorks