Tianhe Xie
Tianhe Xie

Reputation: 261

Normalize space in Selenium python

I'm trying to get product name information from a web. The HTML is as follows:

<div class="p-name p-name-type-2">
    <em xpath="1" style>
        "Apple"
        <font class="skcolor_ljg">iPad</font>
        <font class="skcolor_ljg">Air</font>
        <font class="skcolor_ljg">3</font>
        64G WLAN Grey
    </em>
</div>

I want to get "iPad Air 3 64G WLAN Grey" Previously without selenium, I did this and worked:

product_name_jd = element.css('.p-name-type-2 em').xpath('normalize-space(.)').get()

I'm not sure how the syntax works with Selenium. I tried:

name_temp = element.find_element_by_css_selector('.p-name-type-2 em')
product_name_jd = By.XPATH('//label[normalize-space(text()) =  name_temp]')

But it won't treat name_temp as the variable I defined in the first line. I also tried:

product_name_jd = element.find_element_by_css_selector('.p-name-type-2 em').text
tmallSpider.items = tmallSpider.items['product_name_jd'] = product_name_jd
product_price_jd = element.find_element_by_css_selector('.p-price i').text
tmallSpider.items = tmallSpider.items['product_price_jd'] = product_price_jd
yield tmallSpider.items

But it's giving me this error:

TypeError: 'str' object does not support item assignment

Any idea how i would get the product name?

Upvotes: 1

Views: 200

Answers (1)

frianH
frianH

Reputation: 7563

Try this:

product_name_jd = driver.find_element_by_css_selector('div.p-name.p-name-type-2').text
print(product_name_jd.split('"')[-1].strip())

Upvotes: 1

Related Questions