Dhaval Atri
Dhaval Atri

Reputation: 211

How to find webelement using style property

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. ....

Upvotes: 5

Views: 25549

Answers (4)

Alejandro
Alejandro

Reputation: 122

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 )

Upvotes: 0

Xwris Stoixeia
Xwris Stoixeia

Reputation: 1861

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!

Upvotes: 6

optimistic_creeper
optimistic_creeper

Reputation: 2799

Another way is to use cssSelector as follows:

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

Upvotes: 3

Andersson
Andersson

Reputation: 52665

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;']"))

Upvotes: 4

Related Questions