python - 如何在 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()
提前致谢
解决方案
如果一个不起作用,请尝试使用所有可用的元素标识符,它与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
推荐阅读
- jmeter - 如何在网页测试中将事件合并到一个事务名称下?
- python - Web Scraping, API, HTTPError: HTTP Error 522: Origin Connection Time-out
- teamcity - TeamCity:如何设置正确的触发器文件通配符
- excel - 使用 Excel IF 函数自动更新连接工作表中的值
- javascript - 量角器 - 更新 chrome 版本后获取返回 null 的属性值
- python - 将 groupbys 附加到嵌套字典
- memory - 进程页面存储在哪里 - 在交换空间或文件系统下?请求分页可以在没有空间的情况下工作吗?
- android - Google Architecture Sample 中的 coroutineScope { launch { code } } 和 withContext(iODispatcher) { code } 有什么区别?
- server - 我如何清理我的 Centos7 服务器因为磁盘空间已满
- node.js - 如何为 webapp 创建安全的用户身份验证流程?(VueJS | NodeJS | ExpressJS)