0
votes

I have written a python script which opens seleniums chromedrive. then automatically goes to apples iphone 11 store page, and automatically starts filling in the the options in order to find all the prices.

it does the following

clicks) -> 1) no i don't have a phone to trade in 
clicks) -> 2) pro max 
clicks) -> 3) colour = grey 
clicks) -> 4) clicks on each storage options (64gb. 256gb, 512gb) get each price for all 3 phones and prints them.

problem, selenium doesn't seem to click on the second option propperly. and only prints 2 of the 3 prices i want. why does it not click on the second option?

code below

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.apple.com/uk/shop/buy-iphone/iphone-11-pro')
xpath_string = '//*[@id="tradeup-inline-app"]/div/div/fieldset/div/div[2]/div/div/label/span/span[2]';
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,xpath_string)))
python_button = driver.find_element_by_xpath(xpath_string)
python_button.click()
prices = []
def click(button):
    try:
        time.sleep(1)
        button.click()
        print('clicked   '  + str(button))
    except Exception as e:
        print(str(e))
        print(e)
        print('nope')


for i in range(2):
    if i == 0:
        xpath_string = '//*[@id="Item15_8inch_label"]/span';
        WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,xpath_string)))
        size_button = driver.find_element_by_xpath(xpath_string)
                    click(size_button)
        
        xpath_string = '//*[@id="Item2space_gray_label"]/span[1]/div/img';
        WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,xpath_string)))
        colour_button = driver.find_element_by_xpath(xpath_string)
        
        click(colour_button)

        for j in range(3):
            
            if j == 0:
                xpath_string = '//*[@id="Item3"]/div/fieldset/div/div[1]/div';
                WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,xpath_string)))
                memory_button = driver.find_element_by_xpath(xpath_string)
            if j == 1: ###### this is the thjing its not clicking on (the 256gb button)
                xpath_string = '//*[@id="Item3"]/div/fieldset/div/div[2]/div';
                WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,xpath_string)))
                memory_button = driver.find_element_by_xpath(xpath_string)
            if j == 2:
                xpath_string = '//*[@id="Item3"]/div/fieldset/div/div[3]/div';
                WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,xpath_string)))
                memory_button = driver.find_element_by_xpath(xpath_string)        
            click(memory_button)

            xpath_string = '//*[@id="primary"]/materializer/purchase-options/fieldset/div/div[2]/div/div/div/label/span/span[2]/span';
            time.sleep(0.5)
            WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,xpath_string)))
            price = driver.find_element_by_xpath(xpath_string)
            print(price)
            print(price.text)
            print('ppppppppppppppppppppppppppppppppppp')

        
    else:
        print("wtf")

current output (psuedocode)

<complicated objects?>

£1,049.00

<complicated objects?>

£1,049.00

<complicated objects?>

£1,399.00

it just clearly doesn't click on the second storage option

desired output

<complicated objects?>

£1,049.00

<complicated objects?>

£1,199.00

<complicated objects?>

£1,399.00

actual output

clicked   <selenium.webdriver.remote.webelement.WebElement (session="96d7b3b118d704e050b61ce7dabfa77d", element="a784a06e-e05c-465e-85de-6d22a5cfd4de")>
clicked   <selenium.webdriver.remote.webelement.WebElement (session="96d7b3b118d704e050b61ce7dabfa77d", element="ce0ff50d-3100-4cc2-ae4e-645067317cc8")>
clicked   <selenium.webdriver.remote.webelement.WebElement (session="96d7b3b118d704e050b61ce7dabfa77d", element="0514dd1c-2594-4326-a379-40015580bcbc")>
<selenium.webdriver.remote.webelement.WebElement (session="96d7b3b118d704e050b61ce7dabfa77d", element="5511ea6f-6d8f-463d-a04f-babaa8140b7c")>
£1,049.00
ppppppppppppppppppppppppppppppppppp
Message: element click intercepted: Element <div class="form-choice-selector-label">...</div> is not clickable at point (902, 52). Other element would receive the click: <div class="localnav-tray" id="localnav-tray">...</div>
  (Session info: chrome=81.0.4044.129)

Message: element click intercepted: Element <div class="form-choice-selector-label">...</div> is not clickable at point (902, 52). Other element would receive the click: <div class="localnav-tray" id="localnav-tray">...</div>
  (Session info: chrome=81.0.4044.129)

nope
<selenium.webdriver.remote.webelement.WebElement (session="96d7b3b118d704e050b61ce7dabfa77d", element="5511ea6f-6d8f-463d-a04f-babaa8140b7c")>
£1,049.00
ppppppppppppppppppppppppppppppppppp
clicked   <selenium.webdriver.remote.webelement.WebElement (session="96d7b3b118d704e050b61ce7dabfa77d", element="fb3334de-11ef-4190-9b16-920b72bb06be")>
<selenium.webdriver.remote.webelement.WebElement (session="96d7b3b118d704e050b61ce7dabfa77d", element="5511ea6f-6d8f-463d-a04f-babaa8140b7c")>
£1,399.00
ppppppppppppppppppppppppppppppppppp

Why does it not click on the second option?

1
clicks) -> 2) pro max ; i suppose that's a typo since you're looking for the 5.8" model in your code. - E.Wiest

1 Answers

0
votes

Clicking on the storage options to get the prices is not needed. You can remove your loop. 3 clicks (no trade in + model + color) should be enough.

Once you've clicked, get the prices with :

xpath = "//div[@class='form-selector form-selector-rowwithgutters row']//div[@class='price-point price-point-fullPrice']/span"
tree = browser.find_elements_by_xpath(xpath)
for i in tree:
   print(i.text) 

Also, use relative XPaths for the 3 clicks. In order :

//div[@class="form-choice-selector-label"][./label[@for="noTradeIn"]]
//div[@class="form-choice-selector-label as-dimension-label"][./label[@id="Item15_8inch_label"]]
//div[@class="form-choice-selector-label as-dimension-label"][./label[@id="Item2space_gray_label"]]