Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;href";从标记名(";a";)引发异常_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python &引用;href";从标记名(";a";)引发异常

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

我可以通过tag_name获取链接,但无法从“a”中提取href

引发下面提到的异常的第二行代码

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/"]')))]