我写了一个打开Seleniums chromedrive的python脚本。 然后自动转到Apple iPhone 11商店页面,并自动开始填写选项以查找所有价格。
它执行以下操作
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.
问题,硒似乎没有正确点击第二个选项。 并且只打印我想要的 2 个价格中的 3 个。 为什么它不点击第二个选项?
下面的代码
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")
电流输出(伪码(
<complicated objects?>
£1,049.00
<complicated objects?>
£1,049.00
<complicated objects?>
£1,399.00
它显然没有单击第二个存储选项
期望的输出
<complicated objects?>
£1,049.00
<complicated objects?>
£1,199.00
<complicated objects?>
£1,399.00
实际输出
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
为什么它不单击第二个选项?
不需要单击存储选项来获取价格。您可以删除循环。3次点击(无交易+型号+颜色(应该足够了。
点击后,使用以下命令获取价格:
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)
此外,使用 XPaths 进行 3 次点击。挨次:
//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"]]