Re: [selenium-users] Selenium WebDriver - FindElement By XPath but the Span and Link have visibility Hidden

18,300 views
Skip to first unread message

Krishnan Mahadevan

unread,
Jul 27, 2012, 12:19:00 AM7/27/12
to seleniu...@googlegroups.com
I dont think the issue is with Selenium not being able to find the element. I think the issue is with selenium not being able to operate on the element in question because it is not visible. Perhaps one of its parents is hidden.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



On Thu, Jul 26, 2012 at 10:38 PM, Nathan T <nathan...@gmail.com> wrote:
I am trying to create a test that clicks a menu item in an accordian, but selenium cannot find the element because it is hidden. I am not the developer for the app so i cannot change the code.
 
I would like to click this item:

I am currently using this code

driver.FindElement(By.XPath("//span[contains(.,'Organization')]")).Click();

I have tried a slew of ways to get to this element but i always get the same error from NUnit: OpenQA.Selenium.ElementNotVisibleException : Element is not currently visible and so may not be interacted with.

Is there any way to click this link? Or will it have to be a css change?

 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/TmeU9XrCmtwJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aparna

unread,
Jul 27, 2012, 1:35:37 AM7/27/12
to seleniu...@googlegroups.com
Hello Nathan,

Please try with this line of code.

driver.findElement(By.xpath("//span[text()='Organizations']")).click();

Also are you sure 'Organizations' is not in iframe?

Thanks,
Aparna

Sandy

unread,
Jul 27, 2012, 4:15:02 PM7/27/12
to seleniu...@googlegroups.com
Aparna,

          I am having same problem where I have to click on the hidden element.. and as Krishnan Said Selenium is not able to operate as the parent element are hidden..
So this command given by you will work well in selenium IDE but when you will try the same find.element in webdriver it will fail....

I am still wondering how selenium IDE can do these kind of things very well but web driver doesn't...:(

Peter Gale

unread,
Jul 27, 2012, 5:13:14 PM7/27/12
to Selenium Users
There are several previous threads on this topic.Please search for them if you want to see a comprehensive discussion of the topic.

Date: Fri, 27 Jul 2012 13:15:02 -0700
From: sandeepb...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Selenium WebDriver - FindElement By XPath but the Span and Link have visibility Hidden
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/v6z_BF5NJ5cJ.

Stefan Teixeira

unread,
Jul 30, 2012, 7:09:19 PM7/30/12
to seleniu...@googlegroups.com
Nathan, when the element isn't visible, you can't interact with it in Selenium WebDriver. I suggest you two options:

1 - use WebDriverBackedSelenium, which is an implementation of Selenium RC in WebDriver. Your code would look just like that:

Selenium selenium = new WebDriverBackedSelenium(driver, "http://urlHere");
selenium.click("id=elementId");

driver = ((WrapsDriver) selenium).getWrappedDriver();


(check this blog: http://selenium2advanced.blogspot.com.br/2012/01/advancetopics.html)


2 - use the JavascriptExecutor class, check the API: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html



Regards,
Stefan.



2012/7/26 Nathan T <nathan...@gmail.com>
I am trying to create a test that clicks a menu item in an accordian, but selenium cannot find the element because it is hidden. I am not the developer for the app so i cannot change the code.
 
I would like to click this item:

I am currently using this code

driver.FindElement(By.XPath("//span[contains(.,'Organization')]")).Click();

I have tried a slew of ways to get to this element but i always get the same error from NUnit: OpenQA.Selenium.ElementNotVisibleException : Element is not currently visible and so may not be interacted with.

Is there any way to click this link? Or will it have to be a css change?

 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/TmeU9XrCmtwJ.

Mark Collin

unread,
Jul 31, 2012, 1:28:54 AM7/31/12
to seleniu...@googlegroups.com

I would suggest you make it visible the way a user of the site would make it visible.

 

Interacting with disabled elements is not a true emulation of what the end user is doing and as a result not a realistic test…

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Stefan Teixeira
Sent: 31 July 2012 00:09
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Selenium WebDriver - FindElement By XPath but the Span and Link have visibility Hidden

 

Nathan, when the element isn't visible, you can't interact with it in Selenium WebDriver. I suggest you two options:

1 - use WebDriverBackedSelenium, which is an implementation of Selenium RC in WebDriver. Your code would look just like that:

Selenium selenium = new WebDriverBackedSelenium(driver, "http://urlHere");
selenium.click("id=elementId");

driver = ((WrapsDriver) selenium).getWrappedDriver();

(check this blog: http://selenium2advanced.blogspot.com.br/2012/01/advancetopics.html)


2 - use the JavascriptExecutor class, check the API: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html



Regards,
Stefan.


2012/7/26 Nathan T <nathan...@gmail.com>

I am trying to create a test that clicks a menu item in an accordian, but selenium cannot find the element because it is hidden. I am not the developer for the app so i cannot change the code.

 

I would like to click this item:

Stefan Teixeira

unread,
Jul 31, 2012, 6:03:06 PM7/31/12
to seleniu...@googlegroups.com
Agreed, I just use that when WebDriver really can't interact with some element.



Regards,
Stefan.

2012/7/31 Mark Collin <mark....@lazeryattack.com>

Nathan T

unread,
Aug 2, 2012, 1:04:51 PM8/2/12
to seleniu...@googlegroups.com
It is hidden and the list item above my anchor is hidden. I am currently trying:

Actions userAction = new Actions(driver);
userAction.MoveToElement(driver.FindElement(By.XPath("//span[text()='Organizations']/../.."))).Perform();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
 userAction.Click(driver.FindElement(By.XPath("//span[text()='Organizations']/../.."))).Perform();

the /../.. at the end is to get up to the anchor parent so that i can give it focus. But it seems that MoveToElement is not placing the appropriate focus, or that because of the anchors hidden parent it still cannot find the item.


On Friday, July 27, 2012 12:19:00 AM UTC-4, Krishnan wrote:
I dont think the issue is with Selenium not being able to find the element. I think the issue is with selenium not being able to operate on the element in question because it is not visible. Perhaps one of its parents is hidden.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



On Thu, Jul 26, 2012 at 10:38 PM, Nathan T <nathan...@gmail.com> wrote:
I am trying to create a test that clicks a menu item in an accordian, but selenium cannot find the element because it is hidden. I am not the developer for the app so i cannot change the code.
 
I would like to click this item:

I am currently using this code

driver.FindElement(By.XPath("//span[contains(.,'Organization')]")).Click();

I have tried a slew of ways to get to this element but i always get the same error from NUnit: OpenQA.Selenium.ElementNotVisibleException : Element is not currently visible and so may not be interacted with.

Is there any way to click this link? Or will it have to be a css change?

 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

Nathan T

unread,
Aug 2, 2012, 1:26:00 PM8/2/12
to seleniu...@googlegroups.com


Darrell Grainger

unread,
Aug 2, 2012, 1:53:52 PM8/2/12
to seleniu...@googlegroups.com
Look at it from a real user's perspective. If the element you are trying to click is not visible then you would not click it. You need to make the element visible before you can click it.

For example, if the web page is designed so when a user moves the mouse pointer over an element it fires some javascript which makes a menu appear. The element you want to click is on that menu. In this example, your WebDriver algorithm MUST be:

- move mouse over or click element #1
- wait for menu to appear
- click element #2

Here is an example:

URL url = new URL("http://seleniumserver:4444/wd/hub");
DesiredCapabilities dc = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(url, dc);
driver.get("http://www.kobobooks.com");

// The German link is hidden
WebElement de = driver.findElement(By.xpath("//a[text()='Deutsch']"));

// Find the English link and click it
WebElement eng = driver.findElement(By.xpath("//a[text()='English']"));
eng.click();
// Wait for the German link to become visible
while(!de.isDisplayed()) {
try { Thread.sleep(100); } catch(Exception e) { }
}
// Now we can click it
de.click();
driver.close();

You'll need to figure out what steps a human needs to take to make the element visible then mimic the same steps with WebDriver.

Darrell

Sandy

unread,
Aug 2, 2012, 3:45:50 PM8/2/12
to seleniu...@googlegroups.com
Darell,
           Yes Its right that webdriver behaves in very much similar way as a human do... But while making things visible to able be worked is not easy.
Yes click method is one and there are also actions method but if we are not putting an external wait like thread.sleep the focus used to be lost....
I already had suffered the same with JSF menus. When it comes to selenium IDE it can click the hidden elements directly!!! So either we need to file a bug regarding
lost focus using builder (actions). (Added :- same can not also be achieved using the external wait also...)
                     I haven't filed any bug till now but if more people will counter the similar problems then will need to file it as generalized bug.

Nathan T

unread,
Aug 6, 2012, 1:28:01 PM8/6/12
to seleniu...@googlegroups.com
Darrel,

I have understood that i must develop the test as a user would interact with the site. The problem is that the menu has been created by telerik controls and i cannot find the condition that it is looking for to release the hidden value. This is better described in my Aug 2 posts. I have tried your method, but it did not work. I have also tried other wait techniques all of which still result in the same error. 

Darrell Grainger

unread,
Aug 7, 2012, 3:16:25 PM8/7/12
to seleniu...@googlegroups.com
Go to the page with Firefox or Chrome. Open Firebug (or Inspect in Chrome) on the element you need to click to make the hidden element visible. Look at the DOM before you make the hidden element visible. The anchor you highlight in the Aug 2 post is probably what you want to click on. Write the WebDriver code to click on it. Put a break point in your code and run to that point. Step through the click on the first element. Now use the Watch Window of your IDE to enter what you think should find the hidden element (which should now be unhidden). As you enter things in the Watch Window they will execute. You can use it as a dynamic language, like Python. If the element is still hidden, try inputting different attempts at clicking on the original element to unhide the desired element. My Watch window would have:

// the attempt to unhide the second element | <error>
// the isDisplayed() result of the second element | false

I'd keep tweeking the first entry until the second entry showed true. Then I have the code I need to execute before I can click the hidden element.

Darrell

Nathan T

unread,
Aug 10, 2012, 1:10:12 PM8/10/12
to seleniu...@googlegroups.com
I fixed this particular issue by using IDE. I used WaitFormElementPresent then exported the xml as c#/NUnit/WebDriver and the reference to the particular span was as follows //div[@id='ctl00_menu2_radPanel']/ul/li[3]/div/ul/li/a/span/span[2]". I had not gone so high up the list i had started at  /li/a/span/span[2], but continuously got the cannot select element because it is hidden. I did not like how unspecific the span reference was so i changed it to:

"//div[@id='ctl00_menu2_radPanel']/ul/li[3]/div/ul/li/a/span/span[text()='Organizations']"

I had to use the following code generated by IDE for my test case to work

for (int second = 0;; second++) {
               
if (second >= 60) Assert.Fail("timeout");
               
try
               
{
                   
if (IsElementPresent(By.XPath("//div[@id='ctl00_menu2_radPanel']/ul/li[3]/div/ul/li/a/span/span[text()='Organizations']"))) break ;
               
}
               
catch (Exception)
               
{}
               
Thread.Sleep(1000);
           
}
            driver
.FindElement(By.XPath("//div[@id='ctl00_menu2_radPanel']/ul/li[3]/div/ul/li/a/span/span[text()='Organizations']")).Click();

Thanks you all for your help and suggestions now i'm on to my next issue =)

Reply all
Reply to author
Forward
0 new messages