我如何解决这个错误“webelement 不支持索引"[webdriver][python] [英] how do I get around this error 'webelement does not support indexing"[webdriver][python]

查看:30
本文介绍了我如何解决这个错误“webelement 不支持索引"[webdriver][python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 xpath 搜索

I am doing an xpath search

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

这给了我我需要的第一类项目,在 firebug 中,但显然 python 不允许我将 [1] 添加到 find 函数.有什么解决方法吗?搜索返回 2 个项目,我只想要一个.我这样做是错误的吗?

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?

推荐答案

与大多数 Selenium WebDriver 绑定一样,如果您只指定元素",则只会返回第一个找到的元素.但是,如果您在方法中指定元素",它将返回找到的元素数组.

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.

所以你的代码应该是:

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

参见 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

这篇关于我如何解决这个错误“webelement 不支持索引"[webdriver][python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆