-1
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 语句中收到以下错误:

过时的元素引用:元素未附加到页面文档

4

1 回答 1

1

当您的脚本尝试对其执行某些操作的元素的属性发生更改时,会发生陈旧元素异常。如果要单击带有文本“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
于 2020-08-19T15:02:06.017 回答