connections=driver.find_elements_by_css_selector("a span[class='mn-connection-card__name t-16 t-black t-bold']") print(len(connections))for connection in connections: if connection.text == "XXX": connection.click() break我在 if 语句中收到以下错误:过时的元素引用:元素未附加到页面文档
1 回答
沧海一幻觉
TA贡献1824条经验 获得超5个赞
当您的脚本试图对其执行某些操作的元素的属性发生更改时,会发生陈旧元素异常。如果你想点击带有文本“XXX”的跨度,你可以直接点击它:
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//a[span[text()='XXX']]")))
如果您的要求是遍历所有这些元素,那么:
connections=driver.find_elements_by_css_selector("a span[class='mn-connection-card__name t-16 t-black t-bold']")
print(len(connections))
for i in range(len(connections)):
connections=driver.find_elements_by_css_selector("a span[class='mn-connection-card__name t-16 t-black t-bold']") #Created Fresh element list, so it wont be stale
if connections[i].text == "XXX"
connections[i].click
break
添加回答
举报
0/150
提交
取消