Home > front end >  how to scrape some text inside an href randomly generated - selenium
how to scrape some text inside an href randomly generated - selenium

Time:10-29

I was scraping a dynamic page with selenium and I got stuck getting text 1 and text 2 in the following example:

<span > text 1 <a href="link 1"> text 2 </a> </span>

The same happens if the span is instead a div.


I managed to get text 1 with this python line

var = driver.find_element(By.CLASS_NAME, "class number 1").text"

However, to get text 2, since link 1 is generated, say, randomly, I can't refer to the href in any way!

Any help is really appreciated

CodePudding user response:

Try this, it retrieves 'text 2':

driver.find_element(By.XPATH, ".//span[@class='class number 1']/a").text

CodePudding user response:

Try using CSS Selectors

txt2 = driver.find_element(By.CSS_SELECTOR, "span[class='class number 1'] > a").text

#To extract both text node values at the same time, you can use innerHTML as follows

 driver.find_element(By.CSS_SELECTOR, "span[class='class number 1'].get_attribute('innerHTML')
  • Related