3

I have a web page which displays a dynamic layout (generated by extjs) which is supposed to occupy 100% of the width. I am using

WebElement element = driver.findElement(By.id("ext-gen4"));

String backgroundColor = element.getCssValue("width");

But currently it is returning the width in pixel and on top that the WebDriver browser (Firefox) is not displayed in maximum mode so the value is even less. Is there any way I can get the value in %? I came to know of some GetCssProperty class but I am unable to use it properly. Any details on that? I am using Selenium WebDriver 2.23 with JUnit.

I guess there must be a way to maximize the window.

3
  • "I haven't looked into it." you even admit that? Commented Jul 19, 2012 at 8:27
  • @Frank: Sorry for confusing you folks but my question was about finding the width Css property of a div or anything in % like 100% or 60%. Currently the getCssValue returns the width in pixel. Commented Jul 19, 2012 at 12:11
  • if you mean that please add it to your question Commented Jul 19, 2012 at 13:57

2 Answers 2

2

As Slanec said you can maximize your window like

driver.manage().window().maximize()

Than you can create a method

private float getWidthInRelationToWindow(WebElement element){
    return ((float)element.getSize().getWidth())/
                  this.drivy.manage().window().getSize().getWidth();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. My faith in Stack Overflow is justified. :)
0

Usi this to maximize:

driver.manage().window().maximize()

or even this for fullscreen:

driver.findElement(By.tagName("html")).sendKeys(Keys.F11);

You can't get the value other than in pixels - you'll have to compute the percentage yourself. Note that you don't even have to use the CSS value, there's a getSize() method on a WebElement which can be also used to get the size of <html> or <body> elements, you wouldn't then even need the maximize.

3 Comments

Thanks but that's not the question I was asking. Sorry for confusing you guys. I want to find the width CSS property of a div in %. Currently it returns the value in pixel which renders my test useless in a different system or resolution.
Any idea how can I compute the width in percentage?? Correct me if I am wrong - I get size of the html body, the size of the div and calculate??? Write a function for it??
@MaitreyaDwaipayan As I said, you'll need to calculate it yourself. Just divide the width by the window's (or containing element's) width. Use either getCssValue("width") or getSize().width I gave you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.