Home > Enterprise >  how to make except only one value of some in selenium?
how to make except only one value of some in selenium?

Time:04-18

I want to find title, address, price of some items in an online mall. But, sometimes the address is empty and my code is break in my code(below_it's an only selenium part)

num = 1
while 1:
    try:
        title = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div[' str(num) ']/div/div/a/span').text
        datas_title.append(title)

        address = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div[' str(num) ']/div/div/a/div/p[2]').text
        datas_address.append(address)

        price = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div[' str(num) ']/div/div/a/p').text
        datas_price.append(price)
        print('crowling....num = ' str(num))
        
        num=num 1
    except Exception as e:
        print("finish get data...")
        break

print(datas_title)
print(datas_address)
print(datas_price)

what should I do if the address is empty -> just ignore it and find the next items?

CodePudding user response:

Use this so you can skip the entries with missing information:

num = 1
while 1:
    try:
        title = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div[' str(num) ']/div/div/a/span').text
        datas_title.append(title)

        address = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div[' str(num) ']/div/div/a/div/p[2]').text
        datas_address.append(address)

        price = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div[' str(num) ']/div/div/a/p').text
        datas_price.append(price)
        print('crowling....num = ' str(num))
        
        num=num 1
    except:
        print("an error was encountered")
        continue

print(datas_title)
print(datas_address)
print(datas_price)

CodePudding user response:

address = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div[' str(num) ']/div/div/a/div/p[2]').text
if not address: 
    address = "None"
else: 
    address = address[0].text
datas_title.append(address)

You could use find_elements to check if it's empty and then proceed to do it with either value. You can than encapsulate this into a function pass it the xpath and the data_title array and your code should be repeatable.

  • Related