0

I'm trying to scrap a website using selenium. I tried using XPATH, but the problem is that the rows on the website change over time...

How can I scrap the website, so that it gives me the output '21,73' ?

<div class="weather_yesterday_mean">21,73</div>
c_lab98
  • 5
  • 2

1 Answers1

0

You can just use querySelector that accepts CSS selectors. I personally like them way more than XPath:

elem = driver.find_element_by_css_selector('div.weather_yesterday_mean')
result = elem.text

If that suits you, please read a bit about CSS selectors, for example here: https://www.w3schools.com/cssref/css_selectors.asp

Alex
  • 815
  • 9
  • 19
  • Hi! Thanks for your answer! I tried the following code: elem = driver.find_element(By.CSS_SELECTOR, 'div.weather_yesterday_mean') print(elem.text) I get the error: 'Message: unknown error: unexpected command response' Maybe because there are multiple classes with the name weather_yesterday_mean? – c_lab98 Jul 26 '22 at 08:35
  • I think this is a different error and a different question. Please try to google something about this error, for example https://stackoverflow.com/questions/72758996/selenium-seleniumwire-unknown-error-cannot-determine-loading-status-from-unkn – Alex Jul 26 '22 at 08:37
  • If i know the value, in this example '21.73' can I go trough all the classes and search for this specific value '21.73'? – c_lab98 Jul 26 '22 at 08:37
  • You can search for many *classes* if you know all their names. Please note that several *elements* might have the same class - in this case the function will return all elements matching the condition, and you'll be able to sweep through them and find your desired one: https://stackoverflow.com/questions/18038567/in-selenium-how-do-i-include-a-specific-node-1-using-find-elements-by-css-sel – Alex Jul 26 '22 at 08:40
  • ok thanks. I will have a deeper look into css selecotrs – c_lab98 Jul 26 '22 at 08:51