从这里开始
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#drivers
driver = webdriver.Chrome()
driver.get('https://www.target.com/p/plusone-waterproof-rechargeable-dual-vibrating-massager/-/A-76150669#lnk=sametab')
所以如果船没有找到,我希望addtoCart工作,反之亦然,
ship = driver.find_element_by_xpath('//*[@id="viewport"]/div[4]/div/div[2]/div[3]/div[1]/div/div[3]/div[1]/div[2]/button')
ship.click()
addtoCart = driver.find_element_by_xpath('//*[@id="addToCartButtonOrTextIdFor76150669"]')
addtoCart.click()
time.sleep(2)
cart = driver.get('https://www.target.com/cart')
目前正在学习自动化,想让我自己的机器人:)第一次在这里发帖
以下内容是否满足了您的需求?
from selenium.common.exceptions import NoSuchElementException
try:
ship = driver.find_element_by_xpath('//*[@id="viewport"]/div[4]/div/div[2]/div[3]/div[1]/div/div[3]/div[1]/div[2]/button')
ship.click()
except NoSuchElementException:
try:
addtoCart = driver.find_element_by_xpath('//*[@id="addToCartButtonOrTextIdFor76150669"]')
addtoCart.click()
except NoSuchElementException:
print("Can't click 'Ship' or 'Add to cart'")
time.sleep(2)
cart = driver.get('https://www.target.com/cart')