如果我在特定页面上运行此循环,而代码在循环中确定的位置找不到xPath,我希望循环结束。以下是循环:
for i in range(1, maxListingsForLoops): #@@@ADVERT NUMBER@@@
if i > 10:
break
xPath = "/html/body/main/div[2]/div/div[5]/div[" +str(i)+ "]/div[1]/a[2]"
getRenew = browser.find_elements_by_xpath(xPath)
getRenew = getRenew.get_attribute('href')
#browser.get(getRenew)
print(getRenew)
time.sleep(speed)
当我逐行运行循环时,我可以看到变量是[],猜测这意味着它是空的,这是有意义的,因为在这个页面上没有完整的列表页,所以在五个循环之后,它将不会返回任何内容。 我当前遇到以下错误:
$ xPath = "/html/body/main/div[2]/div/div[5]/div[7]/div[1]/a[2]"
$ print(xPath)
>>>>/html/body/main/div[2]/div/div[5]/div[7]/div[1]/a[2]
$ getRenew = browser.find_elements_by_xpath(xPath)
$ print(getRenew)
>>>>[]
$ getRenew = getRenew.get_attribute('href')
>>>>Traceback (most recent call last):
>>>> File "<input>", line 1, in <module>
>>>>AttributeError: 'list' object has no attribute 'get_attribute'
有人知道当xPath变量返回零、null或空时如何结束循环吗
谢谢
请尝试以下操作:
相关问题 更多 >
编程相关推荐