Unfortunately, I don't think there is enough information to make any alternate suggestions. It would be better if the popup has some visible text that's unique enough then you can use it for identification. Also, when you say popup, do you mean
- A dialog box? Or,
- An alert? Or,
- A lightbox component?
This is one way in which you can get to that ownerId specifically:
It looks like the element's attribute data-atcui-config holds a JSON object as it's data.
Based on the HTML that you posted, you could get a WebElement instance to that node with something along the lines of:
// This is where we will put the final value.
int ownerId;
// Lets get the web element first.
WebElement myElement =
webDriver.findElement(By.cssSelector("span[span-atcui-role='popup']"));
// Now that we have the web element, we'll instantiate a JSONObject for easy parsing.
JSONObject json = new JSONObject(myElement.getAttribute("span-atcui-config"));
// Now lets get the raw url out of that big JSON string.
String url = json.getString("url"); // Get the url
// From here we're just parsing the url as a normal string, this can be done many ways.
Pattern pattern = Pattern.compile("^.*?(?:\\?|&)ownerId=(\\d+)(?:&|$).*$");
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
ownerId = Integer.parseInt(matcher.group(1)); // Get the number from the first group
}
Few notes about this solution:
- I didn't run this through a compiler, so it probably won't work as it is.
- Even if it does compile, my regex is a bit fuzzy and that particular pattern is one I found online, so that may not do what quite I think it does...
What is basically happening here is:
We target the html node we care about by using a CSS selector that targets only that node and getting an instance of the WebElement. Once we have the WebElement, we want to get the value of it's span-atcui-config attribute.
Since we can determine that the attribute's value is JSON, we can instantiate a new JSON Object that we'll use to parse it. The JSON object will let us easily get the URL by calling getString(String key)
to get it's value.
Now we have the URL, and we know that the URL has a parameter called ownerId and has some integer value. So this is where my potentially shoddy regex comes into play, we use a negative look ahead to get the first found series of numbers that come after an instance of 'ownerId'.
Once we have our pattern and matching objects instantiated with our Regex and URL string. We can check to see if it finds our pattern, and then get it as the first group.
Edit: A better way to parse the URL String after you get it might be to follow the accepted answer on this question https://stackoverflow.com/questions/10728093/how-can-i-get-this-url-parameter-with-regex. It uses HttpClient, so you'll need to be able to bring in external dependencies in order to use it.
Edit 2: A better Regex might be [&?]ownerId=(\d+)
for simplicity. Just match it to result group 1.
Hope this helps!