gpt4 book ai didi

python - 在 selenium python 中每 5 秒打印一次移动文本

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:12 25 4
gpt4 key购买 nike

我需要打印每5秒移动一次的文本,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>

我的 Python 代码:

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

以上是获取文本的错误方式。请帮我解决这个问题。

最佳答案

如果你想在文本出现在新的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")

如果您只想打印所有文本节点:

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

关于python - 在 selenium python 中每 5 秒打印一次移动文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53722524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com