我如何在 WebDriverWait 中使用 long CSS_SELECTOR?
How do i use long CSS_SELECTOR with WebDriverWait?
上下文:
——————————————————————
我要按右上角那个小背。我已经从小包里复制了选择器。现在我想让它在我的脚本中按下它。
选择器代码如下所示:
#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg
我试过的:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.z-navicat#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg"))).click()
代码:
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
browser = webdriver.Chrome('C:/Users/rober/OneDrive/Skrivebord/bot/chromedriver')
# Graffik kort
browser.get("https://www.zalando.dk/jordan-air-jordan-1-mid-sneakers-high-joc12n001-a18.html")
buyButton = False
while buyButton is False:
try:
addToCartBtn = addButton = browser.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[4]/button')
print("Varen er udsolgt")
time.sleep(1)
browser.refresh()
except:
addToCartBtn = addButton = browser.find_element_by_xpath('//*[@id="picker-trigger"]')
print("Varen er på Lager")
buyButton = True
while buyButton is True:
time.sleep(3)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.uc-btn#uc-btn-accept-banner"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Vælg størrelse']"))).click()
browser.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Læg i indkøbskurv']"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.z-navicat#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg"))).click()
现在它不起作用,但是如果有任何关于 selenium 和 chromedriver 的聪明人,那么非常欢迎您对我如何解决这个问题发表评论
网站:Link
不需要使用绝对路径只调用元素,就像你通过它的标题属性对“accept-banner”所做的一样:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[title="Indkøbskurv"]'))).click()
或其 href 属性:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[href="/cart/"]'))).click()
或按其 parent class 名称:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.z-navicat-header_navToolItem-bag a'))).click()
上下文:
——————————————————————
我要按右上角那个小背。我已经从小包里复制了选择器。现在我想让它在我的脚本中按下它。
选择器代码如下所示:
#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg
我试过的:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.z-navicat#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg"))).click()
代码:
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
browser = webdriver.Chrome('C:/Users/rober/OneDrive/Skrivebord/bot/chromedriver')
# Graffik kort
browser.get("https://www.zalando.dk/jordan-air-jordan-1-mid-sneakers-high-joc12n001-a18.html")
buyButton = False
while buyButton is False:
try:
addToCartBtn = addButton = browser.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[4]/button')
print("Varen er udsolgt")
time.sleep(1)
browser.refresh()
except:
addToCartBtn = addButton = browser.find_element_by_xpath('//*[@id="picker-trigger"]')
print("Varen er på Lager")
buyButton = True
while buyButton is True:
time.sleep(3)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.uc-btn#uc-btn-accept-banner"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Vælg størrelse']"))).click()
browser.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Læg i indkøbskurv']"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.z-navicat#z-navicat-header-root > header > div:nth-child(3) > div > div > div > div.mrHm0Z.C3wGFf > div > div > div > div.z-navicat-header_topRow > div.z-navicat-header_langNavTools > div > div.z-navicat-header_navToolItem.z-navicat-header_navToolItem-bag > a > div > svg"))).click()
现在它不起作用,但是如果有任何关于 selenium 和 chromedriver 的聪明人,那么非常欢迎您对我如何解决这个问题发表评论
网站:Link
不需要使用绝对路径只调用元素,就像你通过它的标题属性对“accept-banner”所做的一样:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[title="Indkøbskurv"]'))).click()
或其 href 属性:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[href="/cart/"]'))).click()
或按其 parent class 名称:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.z-navicat-header_navToolItem-bag a'))).click()