我正在尝试刷新页面,直到项目出现,但我的代码不起作用(我对其进行了模式:python selenium keep refreshing until item found (Chromedriver))。
以下是代码:
while True:
try:
for h1 in driver.find_elements_by_class_name("name-link"):
text = h1.text.replace('\uFEFF', "")
if "Puffy" in text:
break
except NoSuchElementException:
driver.refresh
else:
for h1 in driver.find_elements_by_class_name("name-link"):
text = h1.text.replace('\uFEFF', "")
if "Puffy" in text:
h1.click()
break
break
这些片段是因为我必须找到一个具有相同类名的项目,并用“”替换BOM(find_element_by_partial_link_text
不起作用)。
for h1 in driver.find_elements_by_class_name("name-link"):
text = h1.text.replace('\uFEFF', "")
if "Puffy" in text:
break
有人可以帮助我吗?非常感谢。
答案 0 :(得分:1)
您正在尝试获取元素列表(driver.find_elements_by_class_name()
可能会返回元素列表或空列表 - 没有例外) - 在这种情况下您无法获得NoSuchElementException
,因此{{1}将不会被执行。请尝试以下
driver.refresh