[英]How can I get text from only <p> and <h2> tags when finding element by class with selenium and python?
我試圖只從 h2 和第一個 p 標簽中獲取文本。 我一直在使用 class 名稱來查找 div,而 output 為我提供了 div 中的所有文本(顯然)。
這是 HTML:
<div class="horoscope-content">
<h2> Today's Libra Horoscope for January 27, 2022 <span class="today-badge">TODAY</span></h2>
<p>Go with the flow, Libra. If you find that a situation isn't unfolding the way you'd like it to, take it as a sign to back off. Swimming upstream is hard work, so use your energy more efficiently by exploring different options. When you step back from a stressful situation, circumstances could turn around. Lighten up by considering other possibilities or talking it through with a helpful friend.</p>
<p>What's in the stars for you tomorrow? <a href="/horoscopes/daily/libra/friday">Read it now</a>.</p>
<div class="dropdown-inline">Read the <b>daily horoscope</b> for another zodiac sign:<div id="dropdown_below_horoscope_dropdown" class="dropdown">
這是我正在使用的代碼:
libra_content = driver.find_elements(By.CLASS_NAME, 'horoscope-content')
我假設答案是使用 xpath 但我不知道如何包含這兩個標簽。 我需要使用兩行單獨的代碼來執行此操作還是可以將兩者合並為一個?
你可以使用:
對於 h2:
libra_content = driver.find_element_by_css_selector("div[class='horoscope-content'] > h2 ")
對於 p:
libra_content = driver.find_element_by_css_selector("div[class='horoscope-content'] > p ")
嘗試這個
<div>
<h2 class="horoscope-content" >........</h2>
<p class="horoscope-content" >........</p>
<p>.......</p>
Libra_content = driver.find_elements(By.CLASS_NAME, 'horoscope-content')
libra_content = [x.find_element(By.XPATH,'./h2[1]').text for x in driver.find_elements(By.CLASS_NAME, 'horoscope-content')]
如果您想同時存儲這兩個值,則可以對這兩個值執行類似的操作。
我使用 css 選擇器解決了它,但沒有將它們組合成一個。 使用 xpath 和 class 名稱結合兩者的另一位評論者的回答是一種可能的解決方案。
libra_h2 = driver.find_element(By.CSS_SELECTOR, 'div.horoscope-content > h2')
libra_p = driver.find_element(By.CSS_SELECTOR, 'div.horoscope-content > p')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.