简体   繁体   English

使用 find_elements 时列表没有属性文本

[英]list has no attribute text when using find_elements

I am scraping the xbox website to get some account information.我正在抓取 xbox 网站以获取一些帐户信息。 In the information, I am trying to scrape someone's name and their status, which fall under the same tag and class, so I made a list to append each of these values to.在信息中,我试图抓取某人的姓名和他们的状态,它们属于相同的标签和类别,所以我制作了一个列表来附加这些值中的每一个。 However, the account does not always have a name attached to it, but always a status so I made an if statement to tell whether there is more than 1 element scraped.但是,该帐户并不总是附有名称,而是始终附有状态,因此我做了一个 if 语句来判断是否有超过 1 个元素被刮掉。 However, it only works if there are two elements;但是,它仅在有两个元素时才有效; not one.不是一个。

status_name = driver.find_elements_by_xpath("//div[contains(@id,'right-side')]/p")

  statusName = []

  if len(status_name) > 1:
    for r in status_name:
      statusName.append(r.text)
    status = statusName[1]
    irlname = statusName[0]
  else:
    status=statusName.text

File "main.py", line 286, in xbox
    statusName.append(status_name.text)
AttributeError: 'list' object has no attribute 'text'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'list' object has no attribute 'text'

elements (with s ) always return list - even for one element - and you don't have to use len() - it will always iterate list, not chars. elements (带有s )总是返回list - 即使是一个元素 - 而且您不必使用len() - 它总是会迭代列表,而不是字符。

BTW: if it can't find elements then it gives empty list and you don't need len() too.顺便说一句:如果它找不到元素,那么它会给出空列表,你也不需要len()

status_name = driver.find_elements_by_xpath("//div[contains(@id,'right-side')]/p")

statusName = []

for r in status_name:
    statusName.append(r.text)

or shorter或更短

status_name = driver.find_elements_by_xpath("//div[contains(@id,'right-side')]/p")

statusName = [r.text for r in status_name]

EDIT:编辑:

You need len() after getting text - to see if it has name获取文本后需要len() - 以查看它是否具有名称

if len(statusName) > 1:
    status  = statusName[1]
    irlname = statusName[0]
else:
    status  = statusName[0]
    irlname = '???'

Eventually you can write all in your way but it will aways need indexes.最终,您可以按照自己的方式编写所有内容,但将需要索引。 And you can do it without list statusName你可以在不列出statusName情况下statusName

status_name = driver.find_elements_by_xpath("//div[contains(@id,'right-side')]/p")

if len(status_name) > 1:
    status  = status_name[1].text
    irlname = status_name[0].text
else:
    status  = status_name[0].text
    irlname = '???'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM