Choosing Only the Visible Element with CSS

Within my program, there exists a text element that appears in two distinct sections: section A and section B (in the form of a popup). My intention was to create one object using CSS that could be utilized in both areas. By doing so, I would be able to employ the same object to evaluate this element regardless of its display location. Unfortunately, I am encountering difficulties in achieving this objective. It seems plausible that I may either need assistance from an individual with stronger expertise in HTML and CSS or that it might not be viable at all.

The following is the HTML code for this element when displayed in section A:

<td id="treeCol" valign="top" style="overflow: hidden; display: block;">
 <div id="orgTreeAndSearch">
  <div class="orgSelection">
   <span id="selection" class="" title="Select an org unit">Select an org unit</span>

The following is the HTML code for this element when concealed in section A (hidden when section B is shown):

<td id="treeCol" valign="top" style="overflow: hidden; display: none;">
 <div id="orgTreeAndSearch">
  <div class="orgSelection">
   <span id="selection" class="" title="Select an org unit">Select an org unit</span>

The following is the HTML code for this element when displayed in section B:

<div class="blockUI blockMsg blockPage PopUp White" style="z-index: 1011; position: absolute; padding: 0px; margin: 0px; width: 1365px; top: 50px; left: 50px; text-align: left; color: rgb(0, 0, 0); border: 0px none; background-color: rgb(255, 255, 255);">
 <div class="White" style="margin: 0px 20px 20px; display: block;">
  <div class="PopUpClose" align="right">
   <div>
    <div align="center">
     <table style="width: 100%;">
      <tbody>
       <tr>
        <td valign="top" align="left" style="width: 410px;">
         <div class="orgSelection">
          <span id="dataAccessOrgSelection" class="">Select org unit(s)</span>

The following is the HTML code for this element when hidden in section B (concealed when section A is shown):

<div class="White" style="margin: 0px 20px 20px; display: none;">
 <div class="PopUpClose" align="right">
  <div>
   <div align="center">
    <table style="width: 100%;">
     <tbody>
      <tr>
       <td valign="top" align="left" style="width: 410px;">
        <div class="orgSelection">
         <span id="dataAccessOrgSelection" class="">Select org unit(s)</span>

In selecting the element within section A, I may utilize its ID with success:

css=#selection

For selection within section B, employing its ID also proves effective:

css=#dataAccessOrgSelection

I aspired to craft a singular selector for this element and thus gave the following approach a try. However, this method targets both the visible and hidden elements. Thus, while on section A, it captures the element referencing both A and B, even though B is obscured (and vice versa).

div.orgSelection span[id]

Is there a means through which I can establish a lone selector for this element that solely pinpoints the observable instance? Perhaps checking for "display:none" within the style attribute could serve this purpose, yet I'm uncertain how to execute this when the element is situated within a td for section A and a div for section B.

Answer №1

So let me clarify your query - you are looking for a CSS selector that is valid for both A and B when they are in a visible state.

td#treeCol[style*=block] span#selection, div.PopUp>div[style*=block] span#dataAccessOrgSelection

To break it down further, the comma represents logical OR in CSS selectors. The visible divs have a portion of their style attribute as 'block' ([style*=block]). Therefore, we are searching for a span with the required id inside a visible div for both selectors. If these selectors don't quite meet your needs, consider tweaking the attributes a bit more.

To ensure the effectiveness of your approach, it's advisable to locate the element using this selector every time before verifying its visibility to prevent any StaleElementReferenceException, given that these elements are distinct.

If I were in your shoes, I would focus on a specific logic rather than hypothetical scenarios. It's crucial to know precisely which element should be visible and under what circumstances.

Answer №2

In accordance with Alexander Petrovich's advice, I would suggest using different element selectors because they represent distinct elements. To facilitate this, you can utilize easy selectors with unique IDs.

However, if there is a valid rationale for employing a single selector for two elements, it is essential to clearly define which parts of the DOM might fluctuate and which remain constant. While my knowledge of CSS is not extensive, I can provide some XPath expressions that may be helpful:

  • //span[(@id='dataAccessOrgSelection') or (@id='selection')]

  • //span[@class='']

  • //span[contains(text(),'Select') and contains(text(),'org unit')]

  • //div[@class='orgSelection']/span

I believe you should be able to translate these XPath selectors into CSS selectors... perhaps referring to this PDF can assist you further: http://code.google.com/p/timger-book/source/browse/trunk/cheatsheet/Locators_table_1_0_2.pdf

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Arranging DIV elements into rows and columns within a table

After working all night to fix a problem, my goal is to create this timetable: https://i.sstatic.net/dAt1J.png Specifically, I'm struggling to find a solution for rows 5, 6, and 7 in the first column. The image is from a program that only runs in IE ...

Hide overflow for images with unique shapes

Here are the two image layers: One shows a hand holding a phone, and the other is a black hole. This is how you can achieve it using HTML markup: <div class="wrapper"> <img class="wrapper__image" src="path/to/image.png"> </div> And ...

Incorporating images into CSS using an npm package

My npm package has the following structure: --src --styles -image.png -style.scss In the style.scss file, the image is referenced like this: .test { background-image: url(./image.png); } The issue arises when consuming the package, as th ...

Displaying items using the flex property can cause the top resize feature to be unreachable

My issue revolves around the display of input errors, which are causing the top section to become inaccessible. I am looking for a solution that will align the content both vertically and horizontally in the center. The challenge arises when the card conta ...

Eliminate the preset padding on the left and right sides of the container-fluid in Bootstrap 5

How can I remove the default padding-left and padding-right in a container-fluid using Bootstrap 5? I don't want to use "!important" in my CSS file. I have already tried disabling the default padding with Chrome dev tools but it didn't work. Any ...

Above, there are two components with a scrollbar, while below there is a fixed div that remains in place when the screen

Just a heads up: below is an example with code of how I envision my interface to look, but I'm not sure if it's valid? I've been trying to create an HTML5/CSS interface that resembles the MIRC fullscreen layout (check out the image below) a ...

Is there a way to create an animation for changing .text in response to an on click event?

I'm currently developing a simple calculator that increases a variable when a button is clicked, displaying the updated value in a div. Each click on the 'amount-upwards' button adds 200 to the displayed number. Below is the jQuery code I a ...

CSS error: Menu hover glitch concealed

I'm currently facing an issue with the navigation on my website. Whenever I place a logo image above the menu, the hover effect of the menu stops working correctly. To clarify: the background doesn't change on hover, but the border does. Here are ...

What is the process for removing the highlighted border from a navigation menu in ASP.NET?

I am currently utilizing the navigation menu in the ASP.NET toolbox, but I am struggling to remove an unwanted golden border. Despite my efforts, I have not been able to find any information on how to resolve this issue. The golden border only appears when ...

JavaScript powered simple window

I am currently working on a basic modal that will open and display content. Unfortunately, I cannot add images to help illustrate the design, so I'll do my best to describe it in detail. The modal is linked to an image slider where the heading change ...

Is there a way to extract the properties of a CSS class from a stylesheet and convert them into a hash using JavaScript or jQuery?

I am exploring a way to extract key value pairs from a CSS stylesheet related to a specific class or id into a JavaScript object for data accessibility. It is important to mention that I do not intend to apply this class directly to any DOM elements. In ...

Using Python's Selenium to capture elements that are loaded lazily

Struggling to click a button using Selenium, but encountering an error: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"AGREE"} This is the button I am attempting to click. https://i.stack.i ...

Ways to turn off Bootstrap links hover background color?

I recently added a Bootstrap menu to my website that I'm currently working on. I am curious about how to disable the hover background color for Bootstrap links. Here's the situation: Below is an example of the menu I implemented: Currently, whe ...

Struggling with applying mixins

Is it possible to select only the top border using this mixin? Currently, I only need a top border but would like to have the option to use others in the future. Using separate mixins for each side of the border seems inefficient. .bordered(@top-width: 1 ...

Sticky positioning causes elements to stick to the window as the

https://i.stack.imgur.com/nv3vU.png I am facing an issue with a position sticky block that can vary in height relative to the window size. Sometimes, the block is taller than the viewport, making it impossible to scroll to see all the content. Is there a ...

Changed the form to become scrollable once it reaches a designated section during scrolling

Seeking assistance! I am currently working on a website using Bootstrap 5, and I have a registration form within the banner section, specifically within a row > col-md-5. The position of the form is fixed, but I want to make it scrollable once it reache ...

Determine the difference between a CSS selector string and an XPath string

Developing a compact querying module (using js) for html is my current project, and I aim to create a versatile query(selector) function that can handle both css selectors and XPath selectors in string form. My dilemma lies in determining whether a given ...

Executing the same class in TestNG with multiple threads simultaneously

Can the same class be run simultaneously with multiple threads, as shown below? <suite name="myTestSuite" verbose="1"> <test name="myTest" parallel="classes" thread-count="5"> <classes> <class name="myPackage.T ...

Placing a logo to the left of a UL list

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> & ...

"Uncovering the Mystery: The Curious Case of My Browser Launching Twice in J

Hey there, I'm a beginner when it comes to Webdriver and Junit. I've been searching for a solution to this puzzle but haven't had any luck so far. I declared my driver as Static Webdriver fd at the top and tried running test cases on single ...