Selenium Xpath 选择器:文本的 find_element 不会打印任何内容

问题描述 投票:0回答:1

在我的脚本中,我尝试选择分页号以便循环。所以,我想获取文本。我尝试打印该内容,但我的终端中没有显示任何内容。我是硒新手,因此我看不出我的代码有什么问题。

print(driver.find_element("xpath", "//div[@class='pagination']/span[@class='pagination-compteur']").text)

这是html代码。

<div class="pagination">
<span class="disabled previous ">
<span class="icon-lt-arrow-left"></span>
<span class="value">Précédent</span>
</span>
<span id="SEL-compteur" class="pagination-compteur"><strong>Page 1</strong> / 3</span>
<a class="link_pagination next pj-lb pj-link" id="pagination-next" title="Page suivante" href="#" data-pjlb="{&quot;url&quot;:&quot;L3BhZ2VzYmxhbmNoZXMvcmVjaGVyY2hlP3F1b2lxdWk9c3lsdmllJm91PUl2cnklMjBzdXIlMjBTZWluZSUyMCUyODk0MjAwJTI5JmlkT3U9TDA5NDA0MTAwJnF1b2lRdWlJbnRlcnByZXRlPXN5bHZpZSZjb250ZXh0ZT04OEhSaFRSdUNXeTBrJTJCWkhUTjdrVlF4SVNNUm5kTkZzVFg4UGclMkJ5bDBpRSUzRCZwYWdlPTI=&quot;,&quot;ucod&quot;:&quot;b64u8&quot;}" data-pjstats="{&quot;idTag&quot;:&quot;PAGE-SUIV&quot;,&quot;pjscript&quot;:&quot;pa.sendEvent('click.navigation',{'click_chapter1':'LEGACY_CLIC','click_chapter2':'MAPPING_CLIC_LR','click':'PAGE_SUIV',idtag:'{%nom}',site_level2:xtn2});&quot;}"> <span class="value">Suivant</span>
<span class="icon icon-lt-arrow-right"></span>
</a>
</div>
python selenium-webdriver
1个回答
0
投票

您的 XPath 表达式看起来不错。请参考以下几个您可以尝试的故障排除步骤:

  1. 检查所需元素是否位于 IFRAME 内。如果是,您需要先切换到

    IFRAME
    ,然后尝试找到并打印所需的文本。请参阅以下答案以了解更多信息
    iframes

  2. 如果没有 IFRAME,则尝试使用 selenium 的 显式等待。请参考以下代码:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    
    ...
    
    print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='pagination']/span[@class='pagination-compteur']"))).text)
    
© www.soinside.com 2019 - 2024. All rights reserved.