GatesOfDelirium
GatesOfDelirium

Reputation: 85

Locating an element in Selenium with a custom value

I am working with a web page that needs some automation and having trouble interacting with certain elements due to their structure. Brief example:

<ul>
<li data-title="Search" data-action="search">
<li class="disabled" data-title="Ticket Grid" data-action="ticket-grid">
<li data-title="Create Ticket" data-action="create">
<li data-title="Settings" data-action="settings">
</ul>

I am aware of all the locator strategies like id and name listed here: http://selenium-python.readthedocs.org/en/latest/locating-elements.html

However, is there a way to specify finding something by a custom value like in this example "data-title"?

Upvotes: 1

Views: 4008

Answers (3)

ro.e
ro.e

Reputation: 319

If there are multiple possibilities it is also worth knowing the following option

from selenium.webdriver import Firefox

driver = Firefox()
driver.get(<your_html>)

li_list = driver.find_elements_by_tag_name('li')
for li in li_list:
    if li.get_attribute('data-title') == '<wanted_value>':
        <do_your_thing>

Upvotes: 2

ddavison
ddavison

Reputation: 29082

You can use CSS to select any attribute, this is what the formula looks like:

element[attribute(*|^|$|~)='value']

Per your example, it would be:

li[data-title='Ticket Grid']

(source http://ddavison.io/css/2014/02/18/effective-css-selectors.html)

Upvotes: 4

Maroun
Maroun

Reputation: 96018

You can use:

"//li[@data-title='Ticket Grid']"

Upvotes: 0

Related Questions