简体   繁体   English

如何在 React 生成的类名上使用 querySelectorAll?

[英]How to use querySelectorAll on a classnames generated by React?

I am trying to find a selector to select tweets from my feed.我试图找到一个选择器来从我的提要中选择推文。 However, Twitter is using React and generates random class names like:然而,Twitter 正在使用 React 并生成随机的类名,例如:

#react-root > div > div > div > main > div > div.css-1dbjc4n.r-aqfbo4.r-1niwhzg.r-16y2uox > div > div > div > div > div.css-1dbjc4n.r-1jgb5lz.r-1ye8kvj.r-6337vo.r-13qz1uu > div > section > div > div > div > div:nth-child(9) > div > div > article > div.css-1dbjc4n.r-18u37iz.r-thb0q2 > div.css-1dbjc4n.r-1iusvr4.r-46vdb2.r-5f2r5o.r-bcqeeo > div.css-1dbjc4n.r-19i43ro > div.css-901oao.r-hkyrab.r-1qd0xha.r-a023e6.r-16dba41.r-ad9z0x.r-bcqeeo.r-bnwqim.r-qvutc0 > span

在此处输入图片说明

I tried to find a pattern allowing me to retrieve the tweets.我试图找到一种模式,允许我检索推文。 Is there a way to retrieve elements with generated by React ?有没有办法用 React 生成的元素来检索元素?

Yes, you can use this one.是的,你可以使用这个。

document.querySelectorAll(".css-901oao.r-hkyrab.r-1qd0xha.r-a023e6.r-16dba41.r- 
    ad9z0x.r-bcqeeo.r-bnwqim.r-qvutc0 > .css-901oao.css-16my406.r-1qd0xha.r-ad9z0x.r- 
    bcqeeo.r-qvutc0")

if you wrote it:如果你写了:

document.querySelectorAll(".css-901oao.r-hkyrab.r-1qd0xha.r-a023e6.r-16dba41.r- 
    ad9z0x.r-bcqeeo.r-bnwqim.r-qvutc0")

this will give you the entire tweet block so you can retrieve username, tweet_id, and so on.这将为您提供整个推文块,以便您可以检索用户名、tweet_id 等。 first query will only give you the tweet text.第一个查询只会给你推文文本。

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

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