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 can';t将元素定位在后面<;span>;在使用xpath或css选择器的网站上_Python_Selenium_Xpath_Css Selectors - Fatal编程技术网

Python can';t将元素定位在后面<;span>;在使用xpath或css选择器的网站上

Python can';t将元素定位在后面<;span>;在使用xpath或css选择器的网站上,python,selenium,xpath,css-selectors,Python,Selenium,Xpath,Css Selectors,在这一点上,我检查了评论的数量-42,我需要抓住它。 它位于这个跨度元素中 <span><i class="tn-icon-comment-dark"> </i>42</span> 输出: None Traceback (most recent call last): File ".\curent_time_playing.py", line 17, in <module> prin

在这一点上,我检查了评论的数量-42,我需要抓住它。 它位于这个跨度元素中

<span><i class="tn-icon-comment-dark">
</i>42</span>
输出:

None
Traceback (most recent call last):
  File ".\curent_time_playing.py", line 17, in <module>
    print(u.text)
AttributeError: 'NoneType' object has no attribute 'text'
无
回溯(最近一次呼叫最后一次):
文件“\curent\u time\u playing.py”,第17行,在
打印(u.text)
AttributeError:“非类型”对象没有属性“文本”

对于这个问题,我有两种方法

  • 获取元素的
    文本
    &将其拆分并根据位置获取数字。默认情况下,它返回
    类型str
    (如果要将其用作整数,则需要键入cast)

  • 您可以直接获取该
    标记的
    xpath
    ,并获取文本。因为内容不是英文的,我不知道你到底在找什么评论


  • 我有两种方法解决这个问题

  • 获取元素的
    文本
    &将其拆分并根据位置获取数字。默认情况下,它返回
    类型str
    (如果要将其用作整数,则需要键入cast)

  • 您可以直接获取该
    标记的
    xpath
    ,并获取文本。因为内容不是英文的,我不知道你到底在找什么评论


  • 作为参考,您可以使用相对XPath:

    (//div[@class="tn-comment-accordion-title"]//span)[1]
    
    输出:

    <span>
    <i class="tn-icon-comment-dark"/>
    31
    </span>
    
    
    31
    

    使用
    .text
    提取注释数。

    可使用的相对XPath作为参考:

    (//div[@class="tn-comment-accordion-title"]//span)[1]
    
    输出:

    <span>
    <i class="tn-icon-comment-dark"/>
    31
    </span>
    
    
    31
    

    使用
    .text
    提取评论数。

    谢谢你,但是我怎么知道我需要split()和0?当我打印“u”时,它一个也没有!前面,当您使用
    get_属性('#text')
    时,它没有返回任何内容,这就是为什么您得到
    None
    。但是如果使用
    text
    方法,它将返回一个由两个单词组成的字符串
    (41一些文本)
    。当您拆分字符串并获取
    索引0
    时,这是预期的结果。谢谢,但是我怎么知道我需要split()和0呢?当我打印“u”时,它一个也没有!前面,当您使用
    get_属性('#text')
    时,它没有返回任何内容,这就是为什么您得到
    None
    。但是如果使用
    text
    方法,它将返回一个由两个单词组成的字符串
    (41一些文本)
    。当您拆分字符串并获取
    索引0
    时,这是预期的结果。