首页 > 解决方案 > 如何在 Selenium Python 中使用 WebDriverWait 触发可点击元素后从弹出窗口中检索数据?

问题描述

我需要从此弹出窗口中抓取图像 src。我已经对此进行了编码,但得到“AttributeError:'NoneType'对象没有属性'findElements'。

这是代码。

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.chrome.options import Options
from chromedriver_py import binary_path

import time
from time import sleep

url = 'https://www.macys.com/shop/product/black-tape-printed-contrast-trim-cardigan?ID=11398979&CategoryID=260&isDlp=true&swatchColor=Neutral%20Animal'

options = Options()
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=options, executable_path=binary_path)
wait = WebDriverWait(driver, 10)
driver.get(url)
sizechart_popup = wait.until(EC.element_to_be_clickable((By.XPATH, './/*[@class="sc-link"]'))).click()
sizechart = sizechart_popup.findElements(By.XPATH('.//*[@id="sizeImg"]/img')).get_attribute("src");

print(sizechart)

# Sleep of 10 seconds irrespective of whether element is present or not
time.sleep(50)
 
# Free up the resources
driver.close()

提前致谢

标签: pythonseleniumselenium-chromedriver

解决方案


如果一个不起作用,请尝试使用所有可用的元素标识符,它与css_selector.

sizechart = driver.find_element_by_css_selector('#sizeImg > img').get_attribute("src")
print(sizechart)

#Output:
https://assets.macysassets.com/dyn_img/size_charts/4011861.gif

推荐阅读