简体   繁体   English

每隔5秒在硒python中打印一次移动文本

[英]Print the Moving text Every 5 Seconds in selenium python

i Need To Print the text which is moving for every 5 seconds,the html code is attacched below: 我需要打印每5秒钟移动一次的文本,以下是html代码:

HTML : HTML:

<span class="cd-words-wrapper" style="width: 1170px;">
    <b class="is-hidden">Test</b>
    <b class="is-hidden">Test</b>
    <b class="is-visible">Test</b>
    <b class="is-hidden">Test</b>
</span>

My Python code: 我的Python代码:

 Text = driver.find_elements_by_xpath(self.header)
        time.sleep(5)
        print(Text.text)

The Above is the Wrong way of Getting the Text. 以上是获取文本的错误方法。 Please help me to sort out this. 请帮我解决这个问题。

If you want to print text once it appear in new b node, try below code: 如果要在文本出现在新的b节点中后对其进行打印,请尝试以下代码:

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

while True:
    text_node = WebDriverWait(driver, 10).until(visibility_of_element_located((By.CSS_SELECTOR, '.cd-words-wrapper > .is-visible')))
    print(text_node.text)
    WebDriverWait(driver, 10).until(lambda driver: text_node.get_attribute('class') == "is-hidden")

If you want just to print all text nodes: 如果只想打印所有文本节点:

for text_node in driver.find_elements_by_css_selector('.cd-words-wrapper > b'):
    print(text_node.get_attribute('textContent'))

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

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