0

When I open a page, there is code that looks like the following:

<div id="policySetup_content">
    <div id="bCS_insureds_contentWrap" style="display: none;">
    <div id="bCS_policy_contentWrap" style="display: block;">
    <div id="bCS_risks_contentWrap" style="display: none;">
    <div id="bCS_rating_contentWrap" style="display: none;">
    <div id="bCS_billing_contentWrap" style="display: none;">
    <div id="bCS_attachments_contentWrap" style="display: none;">
    <div id="bCS_submit_contentWrap" style="display: none;">
</div>

How would I go about getting the @id of whichever one is set to (style="display: block;) inside the @id policySetup_content?

The reason for this is so I can know which page I'm on (because it can be any one of them for various reasons). I need to know the page in order to know which Wrap id to use when working with elements.

Dustin N.
  • 785
  • 1
  • 14
  • 32
  • Selenium doesn't allow selection of hidden elements. So findByCSSSelector using "#policySetup_content div" should return only the element with 'display:block". You can getAttribute of id from there. – Dakshinamurthy Karra Aug 24 '15 at 16:19

2 Answers2

1

Judging by this previou SO question you should be able to use the CSS Selector (div[style*="display:block"]), something along the lines of the below (untested).

String id = driver.findElement(By.cssSelector("div[style*=\"display:block\"]").getAttribute("id");
Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • This is probably the better way to do this but you will probably run into issues with extra/missing spaces in the CSS like `display: block;` vs `display:block;` vs `display : block;` and so on that you will have to work around. Maybe with XPath? – JeffC Aug 24 '15 at 17:10
  • I am getting a completely different id returned with this (i would assume the first one found in the html). Is there a way to start the search within the policySetup_content @id? – Dustin N. Aug 24 '15 at 17:21
  • So I used JeffC's answer above and merged it with this one to get my solution! Thanks to both of you. Here is the final result: String id = driver.findElement(By.cssSelector("#policySetup_content > div[style*=\"display: block\"]")).getAttribute("id"); – Dustin N. Aug 24 '15 at 17:25
  • 1
    Glad it worked... btw, you can replace the `\"` with `'` in the CSS Selector. I find it easier to read. So... `String id = driver.findElement(By.cssSelector("#policySetup_content > div[style*='display: block']")).getAttribute("id");` – JeffC Aug 24 '15 at 17:37
1

Because Selenium will not interact with elements that are not visible, you should be able to pull all the DIVs under the parent DIV and only get the one that is not hidden. I've never tried this approach before but I think it will work...

String id = driver.findElement(By.cssSelector("#policySetup_content > div[id]")).getAttribute("id");

BTW, if you aren't familiar with CSS Selectors this reads find an element with ID (#) policySetup_content that has an immediate child (>) DIV that has an ID. This may need to be tweaked depending on the real HTML that you are dealing with. If it doesn't work, let me know and I can try to help tweak it.

CSS Selector reference

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Using this, it appears as though it's always returning the 1st div id every time and not the one with the style set to display: block – Dustin N. Aug 24 '15 at 17:14
  • I was banking on the fact that Selenium wouldn't pull the elements that it couldn't see but using `.getAttribute()` may go around that. e.g., A way to do the equivalent of `.getText()` on a hidden element is to use `.getAttribute("innerText")`. – JeffC Aug 24 '15 at 17:35