我正在循环遍历行,每个行都有一个链接和一个我分配给它的索引值。除了硒,我还使用Beautiful Soup API检查页面html。
主要问题是,一旦我找到了要使用的链接索引,我就会执行links[index].click()
,它只会偶尔工作。
Error
:list index out of range
当我再次检查时,我发现我的索引仍然在列表的范围内,但仍然不能正常工作
# Each link is confirmed to work, but only works every other time the script is run
page_html = BeautifulSoup(driver.page_source, 'html.parser')
links = [link1, link2]
rows = page_html.find_all('tr',recursive=False)
index = 0
found = False
for row in rows:
col = row.select('td:nth-of-type(5)')
for string in col[0].strings:
# If column has a "Yes" string, let's use the index of this row
if (string == 'Yes'):
found = True
break
# Break from loop if we already have the row that we want
if found:
break
# If not found, continue adding to index value
index += 1
# This is the part of the code that does not work consistently
links[index].click()
为了调试它,我尝试了以下操作:
def custom_wait(num=3):
driver.implicitly_wait(num)
time.sleep(num)
attempts = 0
while attempts < 10:
custom_wait()
try:
links[index].click()
except:
PrintException()
attempts += 1
else:
logger.debug("Link Successfully clicked")
break
当我运行这段代码时,它说链接被成功点击了,但它再次提到索引超出了范围。
如果页面包含2行以上,那么抛出异常也就不足为奇了:O
links
列表包含2个值(index-0
、index-1
(。如果第三个row
的col
不包含字符串"Yes",则不从for
循环中break
,并递增index
变量。
因此,在第三个row
,index
=2,而links
列表在index-2
没有任何内容,因此您得到IndexError
你为什么不在链接上循环呢?
found = False
for link in links:
link.click()
rows = page_html.find_all('tr',recursive=False)
for row in rows:
col = row.select('td:nth-of-type(5)')
for string in col[0].strings:
if (string == 'Yes'):
found = True
break
if found:
break
if found:
break