bpk
bpk

Reputation: 313

Need help finding an element in selenium

Can some one help me to locate an element (without using xpath) which is displayed using : <i id="ext-gen759" class="icon-tool"></i> under a <div> tag. The HTML is as follows:

<div id="ext-comp-1089" class=" MiniTbar">
<a href="javascript:void(0);" id="ext-gen760" class=" active">
<i id="ext-gen759" class="icon-tool"></i>  -->> need to locate this.
</a>

</div>

I don't want to use:

By.Id --> id is dynamic By.XPath --> not stable

I have tried the following without getting a result:

By.className("icon-tool") -- > not working 
By.partialLinkText("icon-tool") --> not working 

Any solution?

Upvotes: 0

Views: 2782

Answers (4)

Aru
Aru

Reputation: 134

You can find it using css selector:

driver.findElement(By.cssSelector("i[class='icon-tool']"));

Upvotes: 0

alecxe
alecxe

Reputation: 473763

You can rely on the part of the id using, for example, starts-with():

//div[starts-with(@id, "ext-comp-")]/a[starts-with(@id, "ext-gen")]/i[@class="icon-tool"]

Or a CSS selector:

div[id^=ext-comp-] a.active[id^=ext-gen] i.icon-tool[id^=ext-gen]

Upvotes: 2

aberna
aberna

Reputation: 5814

Give a chance to the find element by css selector ?

driver.find_element_by_css_selector('i.icon-tool')

The python documentation is here http://selenium-python.readthedocs.org/en/latest/locating-elements.html

Upvotes: 1

Saifur
Saifur

Reputation: 16201

using xpath should do this. You may need to make sure that's the only element i with same criteria on the page

//i[contains(@id,'ext-gen')]

Upvotes: 1

Related Questions