因此,有一个网站Supreme Website,我需要选择一个与关键字变量和颜色变量都匹配的项目。我可以使用这样的关键字轻松选择商品
driver.find_element_by_partial_link_text(keyword.get()).click()
因此,例如,我可以单击Formula Crewneck,但我也希望能够确定颜色。但是,我如何确保它也可以抓住我的color.get()并单击符合两个条件的正确项目。谢谢
这是我到目前为止提供的完整代码,仅供参考
from tkinter import *
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
screen = Tk()
screen.geometry("600x400")
screen.title("Super Bot")
screen.configure(background = "red3")
def web():
name.get()
email.get()
telephone.get()
address.get()
zipad.get()
city.get()
state.get()
number.get()
datem.get()
datey.get()
cvv.get()
category.get()
size.get()
keyword.get()
color.get()
driver = webdriver.Chrome(executable_path=r'C:\Users\artjo\Desktop\Python\chrome\chromedriver.exe')
driver.get('https://www.supremenewyork.com/shop/all/{}'.format(category.get()))
while True:
try:
driver.find_element_by_partial_link_text(keyword.get())
break
except (NoSuchElementException):
wait=WebDriverWait(driver, 10)
waitBis=wait.until(EC.presence_of_element_located((By.ID, 'time-zone-name')))
driver.refresh()
wait=WebDriverWait(driver, 10)
driver.find_element_by_partial_link_text(keyword.get()).click()
name = StringVar()
email = StringVar()
telephone = StringVar()
address = StringVar()
zipad = StringVar()
city = StringVar()
state = StringVar()
number = StringVar()
datem = StringVar()
datey = StringVar()
cvv = StringVar()
category = StringVar()
size = StringVar()
keyword = StringVar()
color = StringVar()
namelabel = Label(screen, width = 10, text = "name", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
namelabel.place(x = 30, y = 62)
nameentry = Entry(screen, textvariable = name, width = 20, bg = "white")
nameentry.place(x = 130, y = 64)
emaillabel = Label(screen, width = 10, text = "e-mail", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
emaillabel.place(x = 30, y = 102)
emailentry = Entry(screen, textvariable = email, width = 20, bg = "white")
emailentry.place(x = 130, y = 104)
tellabel = Label(screen, width = 10, text = "telephone", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
tellabel.place(x = 30, y = 142)
telentry = Entry(screen, textvariable = telephone, width = 20, bg = "white")
telentry.place(x = 130, y = 144)
adrlabel = Label(screen, width = 10, text = "address", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
adrlabel.place(x = 30, y = 182)
adrentry = Entry(screen, textvariable = address, width = 20, bg = "white")
adrentry.place(x = 130, y = 184)
ziplabel = Label(screen, width = 10, text = "zip", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
ziplabel.place(x = 30, y = 222)
zipentry = Entry(screen, textvariable = zipad, width = 20, bg = "white")
zipentry.place(x = 130, y = 224)
citylabel = Label(screen, width = 10, text = "city", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
citylabel.place(x = 30, y = 262)
cityentry = Entry(screen, textvariable = city, width = 20, bg = "white")
cityentry.place(x = 130, y = 264)
statelabel = Label(screen, width = 10, text = "state", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
statelabel.place(x = 30, y = 302)
stateentry = Entry(screen, textvariable = state, width = 20, bg = "white")
stateentry.place(x = 130, y = 304)
numberlabel = Label(screen, width = 10, text = "number", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
numberlabel.place(x = 320, y = 62)
numberentry = Entry(screen, textvariable = number, width = 20, bg = "white")
numberentry.place(x = 420, y = 64)
explabel = Label(screen, width = 10, text = "exp. date", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
explabel.place(x = 320, y = 102)
expentry1 = Entry(screen, textvariable = datem, width = 5, bg = "white")
expentry1.place(x = 420, y = 104)
expentry2 = Entry(screen, textvariable = datey, width = 8, bg = "white")
expentry2.place(x = 470, y = 104)
cvvlabel = Label(screen, width = 10, text = "cvv", bg = "white", fg = "black", font = ("Courier", 10), anchor = W)
cvvlabel.place(x = 320, y = 142)
cvventry = Entry(screen, textvariable = cvv, width = 5, bg = "white")
cvventry.place(x = 420, y = 144)
keywordlabel = Label(screen, width = 10, text = "keyword", bg = "orange", fg = "black", font = ("Courier", 10), anchor = W)
keywordlabel.place(x = 320, y = 222)
keywordentry = Entry(screen, textvariable = keyword, width = 20, bg = "orange")
keywordentry.place(x = 420, y = 224)
colorlabel = Label(screen, width = 10, text = "color", bg = "orange", fg = "black", font = ("Courier", 10), anchor = W)
colorlabel.place(x = 320, y = 262)
colorentry = Entry(screen, textvariable = color, width = 20, bg = "orange")
colorentry.place(x = 420, y = 264)
sizelabel = Label(screen, width = 10, text = "size", bg = "orange", fg = "black", font = ("Courier", 10), anchor = W)
sizelabel.place(x = 320, y = 300)
sizelist = ["S", "M", "L","XL"]
size.set(sizelist[0])
sizemenu = OptionMenu(screen, size, *sizelist)
sizemenu.configure(font = ("Courier", 10),bg = "orange")
sizemenu.place(x = 415, y = 302)
categorylabel = Label(screen, width = 10, text = "category", bg = "orange", fg = "black", font = ("Courier", 10), anchor = W)
categorylabel.place(x = 320, y = 182)
categorylist = ["jackets", "shirts", "tops_sweaters","sweatshirts", "pants", "shorts", "t-shirts", "hats", "bags", "accessories", "skate", "shoes"]
category.set(categorylist[0])
categorymenu = OptionMenu(screen, category, *categorylist)
categorymenu.configure(font = ("Courier", 9),bg = "orange")
categorymenu.place(x = 415, y = 184)
button1 = Button(screen, text="START", command=web)
button1.configure(font = ("Courier", 12), bg = "green", width = 10, height = 2)
button1.place(x=480,y=330)
答案 0 :(得分:0)
肯定可以简化以下xpath(不确定UI如何影响实现)
from selenium import webdriver
d = webdriver.Chrome()
d.get('https://www.supremenewyork.com/shop/all/sweatshirts')
firstMatch = d.find_element_by_xpath('//a[contains(text(), "Crewneck")]//parent::h1/following-sibling::p/a[contains(text(), "Red")]')
print(firstMatch.text)
使用变量:
keyWord = 'Crewneck'
colour = 'Red'
firstMatch = d.find_element_by_xpath('//a[contains(text(), "' + keyWord + '")]//parent::h1/following-sibling::p/a[contains(text(), "' + colour + '")]')
print(firstMatch.text)