Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 为什么硒没有检测到某些标签_Python_Selenium_Selenium Webdriver_Web Scraping - Fatal编程技术网

Python 为什么硒没有检测到某些标签

Python 为什么硒没有检测到某些标签,python,selenium,selenium-webdriver,web-scraping,Python,Selenium,Selenium Webdriver,Web Scraping,我正试图从中提取某些文字 问题:我无法获取与h1或a标记(在图片中突出显示):“HTTP方法--get&POST”相关的特定文本。但是我可以访问类=“条目摘要”并获取它的文本。当我访问标签时,我得到的只是空字符串。为什么? 作为图像的html摘录: 相关代码 main=WebDriverWait(驱动程序,10)。直到( EC.元素的存在位置((By.ID,“main”)) ) #打印(main.text) articles=main。通过标签名称(“article”)查找元素 header.

我正试图从中提取某些文字

问题:我无法获取与
h1
a
标记(在图片中突出显示):“HTTP方法--get&POST”相关的特定文本。但是我可以访问
=“条目摘要”并获取它的文本。当我访问标签时,我得到的只是空字符串。为什么?

作为图像的html摘录:

相关代码

main=WebDriverWait(驱动程序,10)。直到(
EC.元素的存在位置((By.ID,“main”))
)
#打印(main.text)
articles=main。通过标签名称(“article”)查找元素
header.append(articles[0]。通过\u标记\u名称(“a”).text查找\u元素\u)
最低工作代码

从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
导入时间
从随机导入randint
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
url=”https://www.techwithtim.net/?s=test"
##驱动程序设置和访问标题
path=“../dater/driver/chromedriver”
driver=webdriver.Chrome(路径)
获取驱动程序(url)
##从新页面中提取main,然后提取标题
main=WebDriverWait(驱动程序,10)。直到(
EC.元素的存在位置((By.ID,“main”))
)
#打印(main.text)
articles=main。通过标签名称(“article”)查找元素
header.append(articles[0]。通过\u标记\u名称(“a”).text查找\u元素\u)
打印(页眉)

内容在屏幕上可见吗?还是需要向下滚动才能访问它?它可能无法触及,在这种情况下,您需要添加一个向下滚动的功能,例如:

main.execute_script("window.scrollTo(0, 10000)")

也许您可以尝试使用

.get_属性('href')

比如:


希望这对我有用。

[这][类似。建议使用:
标题。附加(article.find_element_by_tag_name(“a”)。get_属性(“textContent”)
而不是
标题。附加(articles[0]。find_element_by_tag_name(“a”)。text)
。但我不明白为什么selenium没有通过
获取此信息。text
它在中不可见,因为我看不到它。但网页已加载它。文本为白色,背景为白色(看起来像)。我可以将鼠标悬停在文本上。我尝试了你的脚本,结果它不再输出我的标题。不确定原因。我不需要
href
。我需要
文本内容
。这很有效。
header.append(articles[0].find_element_by_tag_name("a").get_attribute('href'))
print(header)