0
import pandas as pd
import selenium
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import pandas as pd
import time
driver = webdriver.Chrome(r"C:\Program Files (x86)\chromedriver.exe")
driver.get('https://www.amazon.in/') 
search_item = driver.find_element_by_xpath("//input[@id ='twotabsearchtextbox']")
search_item.send_keys("Laptop")

serch_btn = driver.find_element_by_xpath("//input[@id='nav-search-submit-button']")
serch_btn.click()

cpu_filter = driver.find_element_by_link_text('Intel Core i7').click()
cpu2_filter = driver.find_element_by_link_text('Intel Core i9').click()

href = []
lnks1=driver.find_elements_by_xpath("//a[@class='a-link-normal a-text-normal']")
for lnk in lnks1:
    href.append(lnk.get_attribute('href'))
href = href[0:10]
print(len(href))

Rating = []
for i in href:
    driver.get(i)
    try:
        rating = driver.find_elements_by_xpath("//*[@id='reviewsMedley']/div/div[1]/div[2]/div[1]/div/div[2]/div/span/span")
        for i in rating:
            print(i.text)
    except:
            print('-')

I've tried the above code, but because the 10th element doesn't have any rating, I used try and except. However, it's not working. It's not reverting back the dash(-) and it is just printing the 9 elements.

I want 10th element as dash(-) so that I could match up with at the time of creating dataframe.

I am not getting any idea what to do next. Please help me out with this.

1 Answer 1

0

.text doesn't throw any exception it just retrieves what's present as the text content. If there is no content then it returns an empty string ''

You may use -

for i in href:
    driver.get(i)
    rating = driver.find_elements_by_xpath("//*[@id='reviewsMedley']/div/div[1]/div[2]/div[1]/div/div[2]/div/span/span")
    for i in rating:
       print(i.text if i.text!="" else "-")
            

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.