3
votes

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.

2
"I haven't looked into it." you even admit that? - Franz Ebner
@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. - some_other_guy
if you mean that please add it to your question - Franz Ebner

2 Answers

2
votes

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();
}
0
votes

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.