簡體   English   中英

如何使用 Selenium 和 Python 在網頁上單擊 Continue Checkout gif 元素?

[英]How to click on Continue Checkout gif element on webpage using Selenium and Python?

I am trying to do a tutorial and learn Selenium in python however I cant seem to get Selenium to click the gif for continue checkout, I dont think you will be able to see the page unless you create an account etc so I will paste the html下面的代碼

我在用:

  • Python v3.9
  • 鉻 v87

這是我正在練習的 URL:

https://www.aria.co.uk/myAria/ShoppingBasket

Selenium 代碼:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

import time

# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")

# Open webpage
driver.get("https://www.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054

# Click "Add to Basket" or refresh page if out of stock
try:
    element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
    time.sleep(5)
    browser.refresh()
except:
    button = driver.find_element_by_id("addQuantityButton")
    button.click()

# Click basket
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()

# Click checkout
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(@src,'/static/images/checkoutv2.png')]"))).click()

# Login to account
login = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"login[email]")))
login.send_keys("testuser@hotmail.co.uk")
login.send_keys(Keys.TAB)
driver.implicitly_wait(1)
passwrd = driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[2]/form/div/h1/div[3]/div/div/input[2]")
passwrd.send_keys("password")
driver.implicitly_wait(1)
login.send_keys(Keys.ENTER)

# Click continue checkout
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@src,'/dynres/YnV0dG9uX3NtYWxsLm1lZGl1bQ==/Q29udGludWUgQ2hlY2tvdXQ=.gif')]"))).click()

HTML 我需要點擊的 gif 代碼

<input type="hidden" name="addressID" value="940342">
<div style="text-align:right; margin-top:10px;">
    <input id="formSubmit" class="gaTrackedEvent" class="gaTrackedEvent" data-ga_event_category="checkout" data-ga_event_action="click_continue" data-ga_event_label="deliverydetails" type="image" src="/dynres/YnV0dG9uX3NtYWxsLm1lZGl1bQ==/Q29udGludWUgQ2hlY2tvdXQ=.gif" value="Continue Checkout">
</div>
</form>
</div>

你試過它的ID了嗎? 只要確保就是全部:)“gaTrackedEvent”的 Class 也可能有效。 我需要查看 HTML 的 rest 以確保頁面上的 ID 或 Class 名稱不超過一個。

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "formSubmit"))).click()

xpath 會失敗。

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Continue Checkout']"))).click()

還將密碼更改為

passwrd = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"login[password]")))

要單擊Continue Checkout gif 元素,您需要為element_to_be_clickable()誘導WebDriverWait並且您可以使用以下任一定位器策略

  • 使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.gaTrackedEvent#formSubmit[data-ga_event_category='checkout'][value='Continue Checkout']"))).click()
  • 使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='gaTrackedEvent' and @id='formSubmit'][@data-ga_event_category='checkout' and @value='Continue Checkout']"))).click()
  • 注意:您必須添加以下導入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM