【问题标题】:How can I find elements including certain strings in id with selenium?如何使用 selenium 在 id 中找到包含某些字符串的元素?
【发布时间】:2019-05-09 08:25:57
【问题描述】:

我正在开发一些简单的爬虫来抓取 Twitter 上的转发计数。 我被这个困住了:

<span class="ProfileTweet-actionCountForAria" id="profile-tweet-action-retweet-count-aria-123456789123456789">리트윗 0개</span>

这就是我要收集的目标标签。您可以看到标签的 id 对每个用户都有一些不同的 id 编号。所以我试图像这样用 find_elements_by_xpath 收集那些:

retweets = driver.find_elements_by_xpath("//span[@id='profile-tweet-action-retweet-count-area-*'].text")

我认为 * 在 selenium 中的某些地方有效,但在该代码中不起作用。

那么,简而言之,我怎样才能找到 id 包括 'profile-tweet-action-retweet-count-area' 的元素?

感谢您的关注。我找不到这样的问题(也许我没有用正确的问题搜索它,嗯),但我也找到了很好的参考资料或其他链接!

【问题讨论】:

  • * 不是这样工作的。
  • @cruisepandey 是的,我是这么认为的

标签: python python-3.x selenium selenium-webdriver


【解决方案1】:

Css 选择器是:

span[id*="profile-tweet-action-retweet-count-aria"]  

或者更好的css选择器是:

span[id^='profile-tweet-action-retweet-count-aria']

如果您有多个条目,您可以使用find_elements 方法,该方法将为您提供web 元素列表

如果你不想要 css 选择器并且想坚持使用 xpath:

//span[contains(@id,"profile-tweet-action-retweet-count-aria")]

代码:

list_retweet = driver.find_elements_by_xpath("//span[contains(@id,"profile-tweet-action-retweet-count-aria")]")

for retweet in list_retweet:
  print(retweet.text)

【讨论】:

  • @JeongInKim :您真的应该检查 Ahmed 的答案,我认为这对您不起作用。
  • 我认为在您的代码中,您必须在 contains 方法中使用转义字符或单引号。
  • 没关系,single 和 double 都可以,这取决于编译器如何处理它。
  • 再次感谢 Cruisepandey。奇怪的是它可以获取元素列表,但是只有当我尝试用 .text 打印它们时它才会打印空白......嗯 리트윗 0개 应该打印 리트윗 0개 with .text文字,对吧?
  • 您的 XPath 可以使用 starts-with() 而不是 contains() 来匹配等效的 ^= CSS 选择器。
【解决方案2】:

您可以在 xpath 或 css 选择器中使用 contains()starts-with() 方法。

另外,要从元素中获取文本,您必须在 find_element 方法之外使用 .text

XPath:

retweets = driver.find_elements_by_xpath("//span[starts-with(@id,'profile-tweet-action-retweet-count-area-')]")

或者,

retweets = driver.find_elements_by_xpath("//span[contains(@id,'profile-tweet-action-retweet-count-area-')]")

CSS 选择器:

retweets = driver.find_elements_by_css_selector("span[@id^='profile-tweet-action-retweet-count-area-']")

或者,

retweets = driver.find_elements_by_css_selector("span[@id*='profile-tweet-action-retweet-count-area-']")

您必须迭代列表以获取所有元素,然后您可以使用.text 获取元素的文本

for retweet in retweets:
    print(retweet.text)

编辑: 正如 Cruisepanday 提到的,find_elements_ 返回一个列表,.text 不适用。此外,CSS 选择器不应该有//。我已经相应地更改了代码。

【讨论】:

  • @S Ahmed :列表怎么会有.text 方法。列表在 python 中,.text 在 selenium 中?
  • 谢谢艾哈迈德!感谢您在 css 选择器中使用它的方式。我发现这两个都不能收集隐藏的元素,所以我现在正在努力。
  • 这也是一个无效的 css 选择器。我很惊讶 OP 接受了你的回答。
  • 即使在编辑之后,遗憾的是您的 css 选择器也不正确。
  • @cruisepandey 感谢您在我的回答中发现错误。
【解决方案3】:

使用WebdriverWait 处理动态元素试试Xpath 或Css 选择器

element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,'//span[@class="ProfileTweet-actionCountForAria"][starts-with(@id,"profile-tweet-action-retweet-count-aria-")]')))
print(element.text)

element1=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'span.ProfileTweet-actionCountForAria[id^="profile-tweet-action-retweet-count-aria-"]')))
    print(element1.text)

请注意,您需要使用以下导入。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

【讨论】:

  • 这实际上是我可以在其他问题中使用的那个!哎呀,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多