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?
3 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.
-
@SmartekWorks.. Thanks for your advice. It is quite similar to what I thought initially. But the problem I was facing is to determine relative xPath from Jsoup element. Can you please help me sharing your thoughts on how to write an iteration function to get relative xPath? Commented Apr 9, 2014 at 5:25
-
@SmartekWorks.. Thanks a lot for the idea. While applying this in my project, I have found one more difficulty, when some part of a html page. JSoup parse everything (including the hidden part), but WebDriver does not consider the hidden part. So, sometimes it happens that the xPath, determined from JSoup is invalid for WebDriver, since there are few hidden parts as well. And type="hidden" is not even present in most of the cases. Have you ever faced any similar issue? Commented Apr 15, 2014 at 11:22
-
@AbhishekMukherjee, we never got into such issues, no matter input[type=hidden], or css display:none, all these nodes are counted both in Jsoup and Selenium. The only difference we met is Jsoup element.text() will include invisible text, while selenium getText() only visible text. Commented Apr 16, 2014 at 3:14
-
@AbhishekMukherjee, one more thing, if you found it helpful, kindly please set it as answer, thanks a lot! Commented Apr 16, 2014 at 3:16
You can use xpath selectors to select parent and child elements
Related questions
Select parent using xpath
XML xpath, get the parent element till a specific element
Getting child nodes using xpath?
-
How do I determine xPath of an element using Jsoup, so that I can do WebDriver operation using that xPath? To add more, the web page I'm working having elements of data-dojo-type and the attributes values are changing every 3 seconds auto-refresh. Commented Apr 8, 2014 at 8:58
-
1@AbhishekMukherjee: Havent tried Jsoup but you can directly use Xpath with webdriver to identify parent/child's– AjinkyaCommented Apr 8, 2014 at 9:00
-
@Karna.. The main problem with my project is, no predetermined xPath is possible. It's changing every now and then. So I must traverse using Jsoup. The issue is: even if I get the indented element using Jsoup, I cannot use that element as WebElement to apply WebDriver APIs. Commented Apr 8, 2014 at 9:06
-
@AbhishekMukherjee You can use Xpath expressions in such a way that it wont change (to some extent) even if the HTML changes. Like using id, name or relative location etc– AjinkyaCommented Apr 8, 2014 at 9:08
-
@Karna.. I wish I could do that, but! ID gets changed every 3 seconds auto-refresh. Compound CLASS name not supported by WebDriver. Relative path is not static as well. And I cannot bet on uniqueness of all other element attributes. Commented Apr 8, 2014 at 9:17
What about running findElements
with xpath : .//*
on your particular element? Also, look into xpath parent::*
and following-sibling::*
. For the particular case I understand, there is no need for Jsoup.
-
@Erki.. The main problem with my project is, no predetermined xPath is possible. It's changing every now and then. So I must traverse using Jsoup or any other way. The issue is: even if I get the indented element using Jsoup, I cannot use that element as WebElement to apply WebDriver APIs. Commented Apr 8, 2014 at 9:07