What is the best way to conceal a fixed element that is blocking other elements?

Having trouble clicking on an element due to a drop-down menu covering all other elements.

Incorporating Selenium in Visual Studio, I am attempting to create a testcase where I initially click on a checkbox in a drop-down menu and then proceed to click on another element outside the drop-down. However, the drop-down menu fails to close itself after clicking on the first checkbox.

Manually closing this drop-down in the web browser only requires hitting Esc or clicking somewhere outside the menu. But automation of this process seems to be ineffective.

I attempted to send the Esc key in the script as follows:

Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Escape);

This method did not work as expected. Instead of producing an error for sending the Esc key, it resulted in a timeout when trying to click on the obscured element:

OpenQA.Selenium.ElementClickInterceptedException : Element <div class="mat-radio-outer-circle"> is not clickable at point (116,608) because another element <div class="cdk-overlay-backdrop cdk-overlay-transparent-backdrop cdk-overlay-backdrop-showing"> obscures it

Another attempt involved clicking outside the drop-down instead of using the Esc key:

wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[3]/div[3]"))).Click();

Unfortunately, this approach did not yield results in Visual Studio, even though it worked in Selenium IDE by simply using the 'click' command with //div[3]/div[3] as the target.

View example in Selenium IDE

Various methods such as using the select function in IDE and utilizing firebug have been explored to identify elements outside the drop-down menu. However, none of them proved fruitful besides the specific clickable element mentioned earlier.

Firebug perspective

To sum it up:

  1. Please confirm if my code for sending "Esc" is correct.

  2. Why does Visual Studio struggle to recognize and click on //div[3]/div[3], i.e., outside the drop-down, whereas it is feasible in Selenium IDE?

  3. Are there alternative ways to close the drop-down menu efficiently?

  4. I've come across suggestions about clicking on obscured elements using JavaScript, but I lack guidance on how to implement this in C#. Any recommendations on accomplishing this task?

Answer №1

When it comes to selecting list items, I always opt for using xpath or css directly on the list item instead of clicking on the listbox and selecting. This not only helps in avoiding unnecessary hassle but also makes the process faster. Here is how you can do it:

driver.FindElement(By.Xpath("//select[@attribute='attributeValue']/li[@attribute='attributeValue'])).click

For a Javascript approach, refer to the following 2 links https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IJavaScriptExecutor_ExecuteScript.htm

IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
// if you want to click on element
IWebElement element = driver.FindElement(By.CssSelector("your css locator goes here")) 

// if you want to return value from javascript and store
string title = (string)js.ExecuteScript("return document.title");

If you need to hide an obscuring element, here is a logic utilizing javascript.

element = driver.FindElement(By.Xpath("//div[@class='cdk-overlay-backdrop cdk-overlay-transparent-backdrop cdk-overlay-backdrop-showing']")); // this is the obscuring element in your case.
js.ExecuteScript("arguments[0].setAttribute("style","display: none;");",element);

Answer №2

To resolve this issue, the necessary action was to interact with the element blocking the visibility of the remaining content on the page:

driver.FindElement(By.CssSelector(".cdk-overlay-backdrop.cdk-overlay-transparent-backdrop.cdk-overlay-backdrop-showing")).Click();

Upon performing this step, the dropdown menu successfully closed.

Please be aware that I had to modify the CSSSelector format in order to accurately target the specific element.

In the initial error message provided by Visual Studio, the obstructive element was labelled as follows:

cdk-overlay-backdrop cdk-overlay-transparent-backdrop cdk-overlay-backdrop-showing

However, direct replication of this format within a CSSSelector is not feasible. It is important to prepend each identifier with a ".", and substitute any spaces with "." when constructing a CSSSelector.

For instance:

.cdk-overlay-backdrop.cdk-overlay-transparent-backdrop.cdk-overlay-backdrop-showing

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

Issue with displaying image titles in a responsive image gallery created with HTML and CSS

I am in the process of developing a static website from scratch, coding the HTML and CSS components myself. My goal is to create a responsive "image gallery" that adjusts the width of the images based on the user's screen size. To achieve this, I foll ...

Encountering a StaleElementReferenceException when attempting to interact with buttons in a dynamically updated table using Python with Selenium Webdriver

Encountering a StaleElementReferenceException while testing a webpage with a table is proving to be a challenge. The table includes points and the status of two Blocking points, each with toggle state buttons for 'Yes' and 'No'. The st ...

The fixed navbar in Bootstrap is too wide for the HTML content, resulting in horizontal overflow

My website has a fixed-top navbar that appears wider than my actual HTML tag. I suspect there may be an issue between the navbar and my bootstrap modal, but I am unsure how to resolve it. The wider navbar causes a white space to appear on the right side o ...

What is the solution for getting `overflow-x` to function in place of `overflow-y`?

I have a main container with several sub-containers inside. When the main container's width exceeds its limit, I want to display a horizontal scroll bar. Instead of using the 'display: inline-block' property because it creates unwanted whit ...

Selenium is unable to locate the specified element

Currently, I am utilizing selenium to execute tests that I have developed. Now, my focus is on implementing error handling. I aim to identify the specific element that was not located and log a message like "Element start could not be found" in the error l ...

Background color applied to row elements in div containers

I've been experimenting with integrating table content into CSS floats, but I'm facing some challenges. My current strategy involves using divs at the "table," "row," and "cell" levels. However, I'm not convinced that this is the most effec ...

Patiently awaiting the completion of the entire page loading process

When using the methods below, we can determine if the entire page has finished loading. Sys.sleep(5) or remDr$executeScript("return document.readyState == 'complete';") or remDr$setTimeout(type = "page load", milliseconds = 10000) However, ...

The UL list-style does not seem to be working properly on the Woocommerce checkout page

After creating my e-commerce website and implementing the woocommerce plugin to display products, I encountered an issue. The main menu pages are all showing up with the list-style, but the pages associated with the plugins are not displaying the list-styl ...

The combination of two tags can create a powerful and impactful presence

My task is to place two <h3> tags in the same line or paragraph. <h3>This is the first paragraph. </h3> <h3>This is the second paragraph.</h3> I would like the output to look like this: This is the first paragraph. This is Se ...

Extracting Job Titles from LinkedIn Profiles

Currently, my code is designed to search for job titles on LinkedIn (e.g. Cyber Analyst) and gather all the links related to these job postings/pages. The goal of the code is to compile these links into a list and then iterate through them to print out th ...

The C# method is receiving a null array through Ajax

When I retrieve an array in a loop and pass it through an ajax call to my C# method, I'm encountering a problem where only null values are being received in my parameter. The count of the array is coming through, but the actual data seems to be lost. ...

The ConfigurationManager Class and Real-time configuration updates

Is there a way to detect and respond to any modifications made to the custom application configuration? I'm looking to stay informed of any changes and implement them in my application. ...

Tips for incorporating scrolling into a sub menu

I'm currently attempting to incorporate vertical scroll only for my sub-menu using CSS, without including a horizontal scroll. However, the issue arises when I remove the horizontal scroll - it causes the nested sub-menu within the initial one to bre ...

The webpage's scrollbar has vanished and the div element is unable to stretch to the bottom

I'm in need of some help understanding why my scrollbar has disappeared and why the scrolling area does not reach the bottom. You can find the website in question here. Appreciate any assistance on this matter. Thanks! ...

What are the steps to create a CSS 'shell' design?

How can I create an image displayed as a circle with border-radius:50%, along with some text on the same line that has a set width and background color? I want to achieve this without hard coding values. What is the best approach to do this? Check out the ...

Reimagining scrolling through content with a stylish unordered list (a sleek alternative to a select box

After having our company website redesigned by a professional designer, our site now looks much more visually appealing. However, I have encountered difficulties when trying to implement their design using HTML and CSS. One particular challenge is the heav ...

Error: Angular encountered an undefined variable when attempting to import 'node_modules/bootstrap/scss/grid'

Having some trouble setting up Angular with SCSS and Bootstrap. When attempting to run ng serve, I encountered the following error: ./src/styles.scss - Error: Module build failed (from ./node_modules/sass-loader/dist/cjs.js): SassError: Undefined variable. ...

What is the correct way to implement overflow-y: auto for vertical flex-items?

Check out this interactive example. Here is the HTML code: <div class="overflow"> <div class="block"> <div class="header"> This is a green header </div> <div class="content&q ...

Efficient ASP MVC tool for rendering ViewModel property paths

Let's say we have a MainViewModel with a collection of ItemViewModel, structured like this : public class MainViewModel { public IList<ItemViewModel> Items { get; set; } } public class ItemViewModel { public string Name { get; set; } } ...

When Content Div and Menu Div collide: Resolving HTML and CSS Dilemmas

I am having an issue with my content navigation div overlapping the menu navigation div. Can anyone please help me figure out what I am missing here? You can find the fiddle link below: https://jsfiddle.net/y4c2xs5j/1/ HTML: <div class="top-nav"> ...