简体   繁体   English

Python Selenium 每次打印相同的元素

[英]Python Selenium Printing Same Element Every Time

I'm trying to get tiktok live chat.我正在尝试获取 tiktok 实时聊天。 So i write this code and i figure out live chat messages have a exact class, then i release this code keep spamming first message in the live chat.所以我写了这段代码,我发现实时聊天消息有一个确切的 class,然后我发布了这个代码,继续在实时聊天中发送垃圾邮件。

driver.get("http://tiktok.com/@"+username+"/live")
time.sleep(5)
while True:
    for elem in driver.find_element(By.XPATH,('.//span[@class="tiktok-zsq8pw-SpanChatRoomComment ex6o5349"]')).text:
        print(elem,end="")

How i am gonna pass the next element in body or any solution?我将如何传递正文中的下一个元素或任何解决方案?

In order to iterate over several elements you have to use find_elements instead of find_element and extract the text prom each element during the iteration over the list.为了迭代多个元素,您必须使用find_elements而不是find_element并在迭代列表期间提取每个元素的文本。 Also your while loop is bad there.你的while循环也很糟糕。
So your code could be something like the following:因此,您的代码可能类似于以下内容:

driver.get("http://tiktok.com/@"+username+"/live")
time.sleep(5)
for elem in driver.find_elements(By.XPATH,('.//span[@class="tiktok-zsq8pw-SpanChatRoomComment ex6o5349"]')):
    print(elem.text)

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

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