Selenium WebDriver - getCssValue() method
Yes, all correct.
Here's a screenshot of where to find font-size
through Firebug.
Since the ids are supposed to be unique (at least for this page), you don't need findElements
to find a list of elements with id by-id
and loop through, instead, you use findElement
to get the element directly.
try{
WebElement byId = driver.findElement(By.id("by-id"));
System.out.println(byId.getTagName());
System.out.println("get the text for web element with id='by-id' ");
System.out.println("------------------------------------------------------------");
System.out.println(byId.getText());
System.out.println("------------------------------------------------------------");
System.out.println(byId.getAttribute("id"));
System.out.println(byId.getCssValue("font-size"));
}
}
getCssValue (Color) in Hex format in Selenium WebDriver
Way 1: Using StringTokenizer:
String color = driver.findElement(By.xpath("//div[@class='gb_e gb_f gb_g gb_xb']/a")).getCssValue("color");
String s1 = color.substring(4);
color = s1.replace(')', ' ');
StringTokenizer st = new StringTokenizer(color);
int r = Integer.parseInt(st.nextToken(",").trim());
int g = Integer.parseInt(st.nextToken(",").trim());
int b = Integer.parseInt(st.nextToken(",").trim());
Color c = new Color(r, g, b);
String hex = "#"+Integer.toHexString(c.getRGB()).substring(2);
System.out.println(hex);
Way 2:
String color = driver.findElement(By.xpath("//div[@class='gb_e gb_f gb_g gb_xb']/a")).getCssValue("color");
String[] numbers = color.replace("rgb(", "").replace(")", "").split(",");
int r = Integer.parseInt(numbers[0].trim());
int g = Integer.parseInt(numbers[1].trim());
int b = Integer.parseInt(numbers[2].trim());
System.out.println("r: " + r + "g: " + g + "b: " + b);
String hex = "#" + Integer.toHexString(r) + Integer.toHexString(g) + Integer.toHexString(b);
System.out.println(hex);
Selenium WebElement getCssValue background colour misrepresented?
I have a good theory about why this problem happens, and it seems reproducible.
The blue color is the colour of the elements background when highlighted by a mouse. Selenium seems to pick up that colour instead of the actual background colour of the dropdown option.
Perhaps i should report that as a bug or unintended feature to the devs.
Get CSS property via Appium
According to Appium documentation:
Get Element CSS Value command only applies to webview contexts
https://appium.io/docs/en/commands/element/attributes/css-property/
So it will not work for native context.
Check if element is enabled
There is another way to check if the button is enabled or not:
https://appium.io/docs/en/commands/element/attributes/enabled
MobileElement mobileElement = appiumDriver.findElement(By.xpath("//XCUIElementTypeButton[@name='Get Quote']"));
if(mobileElement.isEnabled()) {
mobileElement.click();
}
Check element color
If you also need to verify the element color, you should take the page (or element) screenshot, then get the element location and try to get the pixel color witing the element area on the screenshot by coordinates.
MobileElement mobileElement = appiumDriver.findElement(By.xpath("//XCUIElementTypeButton[@name='Get Quote']"));
File screenshot = ((TakesScreenshot)appiumDriver).getScreenshotAs(OutputType.FILE);
BufferedImage image = ImageIO.read(screenshot);
Point elementLocation = mobileElement.getLocation();
// Lets take color at point x=10, y=10 inside the element (0, 0 is the top left)
int checkColorX = elementLocation.getX() + 10;
int checkColorY = elementLocation.getY() + 10;
// Optionaly you may try to take not the page screenshot, but just the element screenshot
// File screenshot = mobileElement.getScreenshotAs(OutputType.FILE);
// BufferedImage image = ImageIO.read(screenshot);
// int checkColorX = 10;
// int checkColorY = 10;
// Getting pixel color by position x and y
int color = image.getRGB(checkColorX, checkColorY);
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = color & 0x000000ff;
System.out.println("Red Color value = "+ red);
System.out.println("Green Color value = "+ green);
System.out.println("Blue Color value = "+ blue);
// Then you need to compare each color with the values, you expect.
Get Css Value Width in % - Selenium WebDriver- JUnit
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();
}
Selenium WebDriver getCssValue() is not returning anything for background-repeat-x
Can you test the value of the 'background-repeat' property is 'repeat no-repeat' ?
How to obtain CSS value (eg. color) of element using selenium webdriver in Python
element.value_of_css_property(property_name)
should be the Python's Selenium Webdriver equivalent of getCSSvalue()
.
Documentation can be found on the readthedocs page: Link
Related Topics
How to Make an Array of Arrays in Java
How to Limit the Number of Characters in Jtextfield
How to Use Jndi Datasource Provided by Tomcat in Spring
How Does Facebook Add Badge Numbers on App Icon in Android
Intellij Idea with Junit 4.7 "!!! Junit Version 3.8 or Later Expected:"
How to Automatically Generate Getters and Setters in Eclipse
Issue When Using a Custom Font - "Native Typeface Cannot Be Made"
How to Check Wifi or 3G Network Is Available on Android Device
Android: How to Handle Button Click
Does Java Casting Introduce Overhead? Why
How to Get Eclipse to Use a Different Compiler Version for Java
Getting Jsoup to Support Dynamically Generated HTML by JavaScript
Rxjava Android How to Use the Zip Operator
How to Select and Crop an Image in Android
Android Heterogeneous Gridview Like Pinterest
How to Delete a File on Google Drive Using Google Drive Android API