I am trying to get the text only from the h2 and the first p tag. I've been using class name to find the div and the output gives me all of the text in the div (obviously).
Here is the HTML:
<div >
<h2> Today's Libra Horoscope for January 27, 2022 <span >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 >Read the <b>daily horoscope</b> for another zodiac sign:<div id="dropdown_below_horoscope_dropdown" >
Here is the code I'm using:
libra_content = driver.find_elements(By.CLASS_NAME, 'horoscope-content')
I assume the answer is to use xpath but I can't figure out how to include both tags. Do I need to use two separate lines of code to do it or can I combine both into one?
CodePudding user response:
You could use:
For h2:
libra_content = driver.find_element_by_css_selector("div[class='horoscope-content'] > h2 ")
For p:
libra_content = driver.find_element_by_css_selector("div[class='horoscope-content'] > p ")
CodePudding user response:
you could use :
libra_content = driver.find_elements(By.xpath, 'your_path')
read this:
CodePudding user response:
Try This
<div>
<h2 >........</h2>
<p >........</p>
<p>.......</p>
libra_content = driver.find_elements(By.CLASS_NAME, 'horoscope-content')
CodePudding user response:
libra_content = [[x.find_element(By.XPATH,'./h2[1]').text,x.find_element(By.XPATH,'./p[1]').text] for x in driver.find_elements(By.CLASS_NAME, 'horoscope-content')]
You could do something like this instead for both values if you want to store them both.
