我正在尝试从页面上抓取产品,这引发了以下错误。显然,在第一次单击()时,元素将更新。基本上,我要做的是对每个颜色和大小的项目进行迭代,但是问题出在color.click ()
中。
from selenium import webdriver
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.chrome.options import Options
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.shein.com/Ruffle-Detail-Embroidered-Eyelet-Peplum-Top-p-665095-cat-1733.html')
browser.execute_script('document.querySelector("body > div.welcome-privacy.j-welcome-privacy > div > div.c-modal-wrap.welocme-modal > div.c-modal > div > div > div.modal-body > div > div > button").click()')
WebDriverWait(browser, 15).until(EC.visibility_of_element_located((By.CLASS_NAME, "opt-color")))
colorsForLoop = browser.find_elements_by_class_name('opt-color')
sizesForLoop = browser.find_elements_by_class_name('opt-size')
for color in colorsForLoop:
browser.implicitly_wait(10)
color.click()
for size in sizesForLoop:
size.click()
错误:
---> 17 color.click()
ElementClickInterceptedException:消息:拦截了元素单击: 元素
...不可点击 (1078,381)。其他元素将获得点击:...
欢迎任何帮助,谢谢
答案 0 :(得分:0)
按如下所示更改for循环。
colors = len(browser.find_elements_by_class_name('opt-color'))
for color in range(colors):
browser.implicitly_wait(10)
colorEle = browser.find_element_by_xpath("(//*[@class='opt-color'])[" + str(color+1) +"]")
colorEle.location_once_scrolled_into_view
colorEle.click()
sizes = len(browser.find_elements_by_class_name('opt-size'))
for size in range(sizes):
sizeEle = browser.find_element_by_xpath("(//*[@class='opt-size'])[" + str(size+1) +"]")
sizeEle.location_once_scrolled_into_view
sizeEle.click()