selenium python 文本显示在 pdb 中,但在 print() 中为空

selenium python text shows in pdb but empty in print()

我正在使用 selenium 获取特定元素内的文本。它有时打印文本,有时打印空字符串。但它总是在 pdb 中显示文本。

我尝试了 .text.get_attribute('innerText').get_attribute('textContent') 同时使用 get_elements_by_class_nameget_element_by_css_selector 和 xpath 同样的结果。在 pdb 中始终完美,但在 print()

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome('chromedriver', options=options)
driver.implicitly_wait(4)
driver.get('some url')

no = driver.find_element_by_css_selector('span.hashtag + span')
span = driver.find_elements_by_class_name('live-label-draw')[0].find_element_by_css_selector('span:last-child')
parent = driver.find_elements_by_class_name('live-label-draw')[0]

print(parent.text)
print('-------by class name', span.text, span.get_attribute('innerText'), span.get_attribute('textContent'))

print('-------by css', no.tag_name, no.get_attribute('textContent'), no.get_attribute('innerText'), no.text)

import pdb; pdb.set_trace()

--Insdie pdb--
no.text、no.get_attribute('innerText')、span.text、span.get_attribute('innrText') 所有打印 31907040069
还 parent.text 打印 #31907040069

<span class="live-label-draw">
  <span class="hashtag">#</span>
  <span>31907040069</span>  <--- trying to get '31907040069'
</span>

预计:
#31907040069
------按class 姓名 31907040069 31907040069 31907040069
------来自 css 31907040069 31907040069 31907040069

实际结果:
#
------来自 class
------来自 css

我只需要使用 WebDriverWait().until 显式等待,直到元素可见。
这就是它在 pdb 中工作的原因,因为它会自动停止并等待。