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_Beautifulsoup - Fatal编程技术网

Python 检查元素是否存在于页眉、正文或页脚中

Python 检查元素是否存在于页眉、正文或页脚中,python,selenium,selenium-webdriver,beautifulsoup,Python,Selenium,Selenium Webdriver,Beautifulsoup,以本网站为例: 联系人号码显示在webiste的正文中 某些站点将其与菜单选项卡一起放在顶部,某些站点位于底部页脚 我已经开发了一个例程,使用 element.location element.location_once_scrolled_into_view 并滚动到元素的视图中 browser.execute_script("arguments[0].scrollIntoView()", element) 是否有一种方法可以直接解释元素是否存在于网页的页眉/正文/

以本网站为例:

联系人号码显示在webiste的正文中 某些站点将其与菜单选项卡一起放在顶部,某些站点位于底部页脚

我已经开发了一个例程,使用

    element.location
    element.location_once_scrolled_into_view
并滚动到元素的视图中

    browser.execute_script("arguments[0].scrollIntoView()", element)
是否有一种方法可以直接解释元素是否存在于网页的页眉/正文/页脚中,借助于标签,使用selenium或python中的bs4

编辑

标题示例:

页脚示例:

你能不能不只是使用
元素。父项
循环,直到找到一个目标标记

像这样:

from bs4 import BeautifulSoup as soup

html = """<html><header><div><span class="phone">123456789</span></div><body><div></div><footer><div></div></footer>"""
location = ['header','body','footer']
page = soup(html, 'html.parser')

element = page.find('span',{'class':'phone'})

while (element.parent):
    if element.parent.name in location:
        print("Phone is in " + element.parent.name)
        break
    else:
        element = element.parent
从bs4导入BeautifulSoup作为汤
html=“”123456789”“”
位置=[‘页眉’、‘正文’、‘页脚’]
page=soup(html,'html.parser')
element=page.find('span',{'class':'phone'})
while(element.parent):
如果element.parent.name位于以下位置:
打印(“电话在”+元素中。父元素。名称)
打破
其他:
element=element.parent
编辑:

要同时检查类名,请执行以下操作:

from bs4 import BeautifulSoup as soup

html = """<html><header class='test-class'><div><span class="phone">123456789</span></div><body><div></div><footer><div></div></footer>"""
location = ['header','body','footer']
soup = BeautifulSoup(html, 'html.parser')

element = soup.find('span',{'class':'phone'})

while (element.parent):
    if element.parent.name in location and 'test-class' in element.parent.get('class'):
        print("Phone is in " + element.parent.name)
        break
    else:
        element = element.parent
从bs4导入BeautifulSoup作为汤
html=“”123456789”“”
位置=[‘页眉’、‘正文’、‘页脚’]
soup=BeautifulSoup(html,'html.parser')
element=soup.find('span',{'class':'phone'})
while(element.parent):
如果位置中的element.parent.name和element.parent.get('class')中的“test class”:
打印(“电话在”+元素中。父元素。名称)
打破
其他:
element=element.parent

谢谢您的建议。parent.name是否查找标记名或类名?我想我们可以使用element.parent['class']作为类名。如何获取父类的class/id名称?@LakshmiNarayanan您可以在
元素上添加
get('class')
。parent
。小心,它会返回一个列表。查看我的编辑:-)谢谢@Maaz的编辑。我刚刚意识到,我查找元素的例程更通用于selenium。因此,我使用selenium的get_属性()转换了您的例程。是否有一种方法可以使用selenium获取元素的标记,类似于使用bs4获取.name?