我正在尝试做一个教程,并在python中学习Selenium,但我似乎无法让Selenium单击" checkout ";使用"element_to_be_clickable((By.XPATH")
我正在使用:
Python v3.9
Chrome v87
这是我正在练习的URL:
https://www.aria.co.uk/myAria/ShoppingBasket
这是我当前的点击代码:
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:UsersSte1337Desktopchromedriverchromedriver.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()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
checkout = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH("//img[contains(@src,'/static/images/checkoutv2.png.png')]"))).click()
我可以看出你的xpath
是不正确的。
您的Xpath应该是。
//img[contains(@src,'/static/images/checkoutv2.png')]
你的代码应该是。
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(@src,'/static/images/checkoutv2.png')]"))).click()
您提供的链接包含hCaptcha,它实际上负责检查你是人类还是机器人。我想这也是为什么你不能点击页面上的任何项目的原因,因为Selenium实际上就是一个机器人。
您首先必须通过点击要求的图像来通过测试。