Python &引用;href";从标记名(";a";)引发异常
我可以通过tag_name获取链接,但无法从“a”中提取href 引发下面提到的异常的第二行代码Python &引用;href";从标记名(";a";)引发异常,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我可以通过tag_name获取链接,但无法从“a”中提取href 引发下面提到的异常的第二行代码 StaleElementReferenceException: stale element reference: element is not attached to the page document 您在尝试获取HREF时获得了所有链接,其中一些链接发生了更改,这就是StaleElementReferenceException的原因。您可以尝试等待具有必需href的链接 from seleni
StaleElementReferenceException: stale element reference: element is not attached to the page document
您在尝试获取HREF时获得了所有链接,其中一些链接发生了更改,这就是
StaleElementReferenceException
的原因。您可以尝试等待具有必需href的链接
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ...
names = [link.get_attribute('href') for link in WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'a[href$=".com/"]')))]
在上面的示例中,由
a[href$=”.com/“]
从所有页面获取的链接,而不是作为滚动框
子项。如果要使用滚动框
,请将定位器修改为滚动框
选择器a[href$=“.com/”]或使用自定义等待或实现自定义预期条件
“链接”是否包含“.com/”以外的类型值?你能发布实际的url吗?我建议使用更具体的定位器(如类、名称等)查找链接。实际上,我正在进行instagram“follower/following”列表数据提取。我认为“链接”中还有其他链接。我不能指定“类”,因为它在instagram网站上不断变化。我必须使用scroll\u box,因为我只想提取scroll\u box的信息。我正在使用instagram网站并提取关注者/以下列表。共享滚动框的html
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ...
names = [link.get_attribute('href') for link in WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'a[href$=".com/"]')))]