Python – how to get around this error ‘webelement does not support indexing”[webdriver][python]

pythonseleniumwebdriver

I am doing an xpath search

page = driver.find_element_by_xpath('//td[@class="mceIframeContainer mceFirst mceLast"]')[1]

This gives me the first class item which I need, in firebug, but apparently python won't allow me to add the [1] to the find function. Is there any workaround? The search returns 2 items, I only want one. Am I approaching this wrong?

Best Solution

Like most of the Selenium WebDriver bindings, if you only specify "element", you will only return the first found element. However, if you specify "elements" in your method, it will return an array of elements found.

So your code should be:

page = driver.find_elements_by_xpath('//td[@class="mceIframeContainer mceFirst mceLast"]')[1]

See http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html?highlight=find_element_by_xpath#selenium.webdriver.remote.webdriver.WebDriver.find_elements_by_xpath