How to find webelement using style property
Asked Answered
M

4

5

Below is the HTML code of an element and I want to locate this element by classs and style property using selenium webDriver in java

 <div class="qooxdoo-table-cell" style="left:252px;width:117px;height:24px;"/>

suggest a way which can be help full in selenium

I want to locate the element using java code i.e. Driver.findelement(by. ....

Mcelhaney answered 21/10, 2016 at 9:11 Comment(2)
use this css selector "div.qooxdoo-table-cell"Pivoting
yup but i have to use style property bcoz there are many element having the same class name and that i don't have idea.Mcelhaney
C
6

As long as the element isn't unique you must grab both attributes:

This is the general form, replacing the empty strings for your required class and style:

driver.findElement("By.xpath(//div[@class='' and style='']");

So:

driver.findElement(By.xpath("//div[@class='qooxdoo-table-cell' and style='left:252px;width:117px;height:24px;']");

Best of luck!

Chlor answered 21/10, 2016 at 9:27 Comment(1)
You missed " before By.xpath in the first code snippet.. driver.findElement("By.xpath(//div[@class='' and style='']"); not driver.findElement(By.xpath(//div[@class='' and style='']");Liss
E
4

If you need to match <div> with exact style attribute, you can try something like

driver.findElement(By.xpath("//div[@class='qooxdoo-table-cell'][@style='left:252px;width:117px;height:24px;']"))
Emlynne answered 21/10, 2016 at 9:15 Comment(1)
yup but i have to use style property bcoz there are many element having the same class name and that i don't have ideaMcelhaney
B
3

Another way is to use cssSelector as follows:

driver.findElement(By.cssSelector("div[style='left:252px;width:117px;height:24px;']"));
Buster answered 21/10, 2016 at 9:25 Comment(0)
H
0

If you are using a older version of selenium you can as well use the older version of cssSelector.

driver.find_elements_by_css_selector("div[style='left:252px;width:117px;height:24px;']")

Note: this function still works in current version of Selenium (with DeprecationWarning )

Huss answered 8/5, 2022 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.