选择html文本代码的特定文本部分

2024-10-02 04:28:29 发布

您现在位置:Python中文网/ 问答频道 /正文

请问,如何从这个html中获取文本“没有事件,请稍后再试”

...</option></select><!-- --></div><div class="media_container__no-match-tracker"><div><div><div class="icon icon--football icon--size_auto"></div><div class="icon icon--volleyball icon--size_auto"></div><div class="icon icon--handball icon--size_auto"></div><div class="icon icon--hockey icon--size_auto"></div><div class="icon icon--basketball icon--size_auto"></div><div class="icon icon--tennis icon--size_auto"></div></div> There is no event, try later. </div></div></div></div></div><div class="app_column__bottom_row" data-v-eb1959b8=""><div class="app_column__bottom_row_wrapper" data-v-eb1959b8=""><div class="app_column__bottom_row_content" data-v-eb1959b8=""><div class="tab_menu chatMenu" data-v-4d018892="" data-v-eb1959b8=""><div ...

我尝试:

driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[2]/div/div/div[1]/div/div[2]/div[2]/div')

但只返回:

<selenium.webdriver.remote.webelement.WebElement (session="d79e3bdd44586c81ae5922410f0c20dd", element="405526fe-7e1a-4552-af5a-70cfb341e129")>

我使用Python3,硒,bs4,无头铬。多谢各位


Tags: no文本divappautodatasizehtml
2条回答

阅读代码和html有点困难,但请尝试以下xpath定位器:

//div[@class='media_container__no-match-tracker']

并返回元素的文本:

driver.find_element_by_xpath('//div[@class="media_container__no-match-tracker"]').text

@DMart

谢谢,这是我需要的。我能再问一个问题吗?站点是动态的,所以这个元素并不是每一个代码中我都能找到的。如果没有,这个驱动程序。找到。。。返回:

        driver.find_element_by_xpath('//div[@class="media_container__no-match-tracker"]').text
Traceback (most recent call last):



  File "<ipython-input-95-8dcfa123bd2a>", line 1, in <module>
    driver.find_element_by_xpath('//div[@class="media_container__no-match-tracker"]').text

  File "C:\Users\Miro\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)

  File "C:\Users\Miro\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']

  File "C:\Users\Miro\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)

  File "C:\Users\Miro\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="media_container__no-match-tracker"]"}
  (Session info: headless chrome=86.0.4240.193)

我怎样才能把它作为条件呢?如果元素在站点上,则打印它,如果不打印(“元素不在此url上”)

相关问题 更多 >

    热门问题