F
F
Feomatar4eg2020-05-08 11:22:05
Python
Feomatar4eg, 2020-05-08 11:22:05

Python. selenium. Extra information is being parsed, how to correct the path?

The html code for the page is as follows:

<div class="product_item__description ">
  <div>
        9 000&nbsp;
    <span class="rub">
    <!--noindex-->
    <b>&#8381;</b>
    <!--/noindex-->
    <i class="rub__old">руб.</i></span>
  </div>
  <div class="product_item__old-price">
                  10 000&nbsp;<span class="rub">
          <!--noindex--><b>&#8381;</b>
          <!--/noindex-->
          <i class="rub__old">руб.</i></span>
  </div>
</div>

I'm trying to parse just this: "9 000" with the following code:
prices= driver.find_elements_by_css_selector("div.product_item__description > div")


We get an array of prices in prices. In most cases, no, then everything is fine, the number of goods = the number of prices in the prices array, but when old-price is present, then 2 prices " 9 000 " and 10 000 are written to the prices array.

Here is the actual question of how to make selenium take data only from the first div and the second where the old price is ignored

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2020-05-08
@dimonchik2013

the second one has a special class ))
product_item__old-price
choose div without it and the whole
second option - just take the first div, but it's crooked if it turns out to be empty

S
Sergey Karbivnichy, 2020-05-08
@hottabxp

Refer to the first div element:

prices= driver.find_elements_by_css_selector("div.product_item__description > div")
print(prices[0].text)

Just tried it, it works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question