Pycharm报错 TypeError: 'WebElement' object is not subscriptable
报错信息:
Traceback (most recent call last):
File "D:\PycharmProjects\ExcavatorPro\testPro.py", line 100, in <module>
for tr in table.find_element(By.TAG_NAME, 'tr')[2:]:
TypeError: 'WebElement' object is not subscriptable
解决方法:
在代码第100行中,
for tr in table.find_element(By.TAG_NAME, 'tr')[2:]:
element元素少了s,这会使定位出现问题,需要把find_element改成find_elements进行定位,才能解决问题
修改后代码:
for tr in table.find_elements(By.TAG_NAME, 'tr')[2:]:
tds = tr.find_elements(By.TAG_NAME, 'td')
print(tds[4].text,tds[5].text,tds[6].text,tds[7].text,tds[8].text,tds[12].text,tds[13].text,tds[15].text)
print()