find_elements_by_xxx () usage and error TypeError: 'FirefoxWebElement' object is not subscriptable

A, find_elements_by_xxx () action

Recent study selenium automated web testing, using tag_name elements positioned when he met a problem is when the same label table little more time, the default value is returned first tab, then select how other labels it, this time need use find_elements_by_xxx (). This function returns a list of available target query form you have all matching elements, then you can use the list to select elements select as the next table.

Second, in order to locate Baidu link as an example tag_name

code show as below:

#导包
from selenium import webdriver
from time import  sleep
#定义火狐浏览器驱动
driver=webdriver.Firefox()
#链接
url="https://www.baidu.com/"
#浏览器打开链接
driver.get(url)
#定位查询输入框元素
driver.find_elements_by_tag_name('input')[7].send_keys('身份证识别')
#暂停3秒钟
sleep(3)
#关闭浏览器
driver.quit()

Check input box was the source of input is the eighth, and so is access [7] position.

Most started with an error TypeError: 'FirefoxWebElement' object is not subscriptable

The reason behind the search a bit because I find_elements_by_tag_name (); I wrote find_element_by_tag_name (). oh s one less element. So it can not lead to the return of all value.

Published 84 original articles · won praise 18 · views 10000 +

Guess you like

Origin blog.csdn.net/sunandstarws/article/details/104497939