使用python的不可靠的硒webdriver单击

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python的不可靠的硒webdriver单击相关的知识,希望对你有一定的参考价值。

我写了一个python脚本,可以打开硒chromedrive。然后自动进入苹果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.

问题,硒似乎没有正确点击第二个选项。并且只打印我想要的3个价格中的2个。为什么不单击第二个选项?

下面的代码

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) 

[另外,请使用相对XPath进行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"]]

以上是关于使用python的不可靠的硒webdriver单击的主要内容,如果未能解决你的问题,请参考以下文章

提交元素不可点击的硒python

弹出窗口中的硒选择按钮

如何单击一个我看不到它的元素的硒框?

在python中使用没有选择标签的硒专门选择谷歌表单下拉列表

python中的硒:NoSuchElementException:消息:没有这样的元素:无法找到元素

如何使用 selenium webdriver 单击此按钮?