1

Using IWebElement.Location, I can get the coordinates of the upper-left corner of an element relative to the upper-left corner of the page.

But this location doesn't tell me where the element is located in the current browser window, that is, whether it is off screen or on screen and if onscreen, then whether near the top or bottom or middle etc. Is there any way to get this info in C#?

Thanks!

Edit:
For context, the problem I'm trying to solve is something like this:
1. Create a Selenium WebDriver instance and go to the page http://en.wikipedia.org/wiki/Selenium_(software)
2. Click on a link on the Contents table, say Selenium IDE.
I need to verify that clicking on the link takes me to the Selenium IDE section on the page, that is, the Selenium IDE section is now displayed near the top of the browser window.

Srinivas
  • 11
  • 1
  • 5

2 Answers2

0

Long overdue, what you are looking for is Displayed.

driver.FindElement(By.Id("MyID")).Displayed

Or, in your case:

IWebElement.Displayed;
Machtyn
  • 2,982
  • 6
  • 38
  • 64
  • `IWebElement.Displayed` returns true irrespective of whether the element is on-screen or off-screen. That doesn't help me much. I want to know the location of the element within the current displayed screen. I want something similar to `IWebElement.Location` but which changes as I scroll up or down the page. – Srinivas Apr 20 '15 at 10:30
0

Perhaps this is the answer you are looking for. You'll likely have to rely on javascript. Here's where I got the idea: JQuery get the location of an element relative to window

Or, wait! You can do this with pure Selenium. (Same basic idea as in the link.)

  1. Get the position of the element, which you know: IWebElement.Location
  2. Get browser window information a. Get the size of the window: IWebDriver.Manage().Window.Size b. If that doesn't work, you'll have to use (IJavaScriptExecutor)IWebDriver.ExecuteScript("arguments[0].scrollTop", IWebElement); or whatever the equivalent javascript executor command is to Firefox's console command of $(window).scrollTop()
  3. Do some math: window size x - browser info x. window size y - browser info y
  4. If it's negative, it's off the screen up top. If it's positive, but greater than the window size, then it's off the screen low.
Community
  • 1
  • 1
Machtyn
  • 2,982
  • 6
  • 38
  • 64