[英]Selecting specific text part of html text code
Please, how can I get text "There is no event, try later."请问,我怎样才能收到文本“没有活动,请稍后再试”。 from this html?
从这个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 ...
I try:我尝试:
driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[2]/div/div/div[1]/div/div[2]/div[2]/div')
but return only:但只返回:
<selenium.webdriver.remote.webelement.WebElement (session="d79e3bdd44586c81ae5922410f0c20dd", element="405526fe-7e1a-4552-af5a-70cfb341e129")>
I use python3, selenium, bs4, headless chrome.我使用 python3、selenium、bs4、无头铬。 Thank you.
谢谢你。
A bit hard to read your code and html but try this xpath locator:阅读你的代码和 html 有点困难,但试试这个 xpath 定位器:
//div[@class='media_container__no-match-tracker']
And to return the text of the element:并返回元素的文本:
driver.find_element_by_xpath('//div[@class="media_container__no-match-tracker"]').text
@DMart @DMart
Thank you, this is what I need.谢谢,这正是我需要的。 Can I one more question?
我可以再问一个问题吗? Site is dynamical, so this element is not in code everityme I scrape it.
站点是动态的,所以这个元素不在我抓取的代码中。 When it doesnt, thist driver.find... return:
当它没有时,thist driver.find... 返回:
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)
How I can use this as condition?我如何使用它作为条件? If element is on the site print it, if not print("element isnt on this url").
如果元素在站点上,则打印它,否则打印(“元素不在此网址上”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.