我在代码中添加了隐式等待,它会导致错误“Message:过时的元素引用:元素未附加到页面文档”
下面是代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
service_obj = Service("C:/Users/divya/Downloads/chromedriver_win32/chromedriver.exe")
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service_obj)
driver.implicitly_wait(5)
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/")
driver.find_element(By.CSS_SELECTOR, ".search-keyword").send_keys("ber")
# time.sleep(2)
products = driver.find_elements(By.XPATH, "//div[@class='products']/div")
count = len(products)
assert count > 0
for product in products:
product.find_element(By.XPATH, "div/button").click() #=======ERROR HERE=============
driver.find_element(By.CSS_SELECTOR, ".cart-icon").click()
driver.find_element(By.XPATH, "//button[text()='PROCEED TO CHECKOUT']").click()
# time.sleep(3)
driver.find_element(By.XPATH, "//input[@type='text']").send_keys("rahulshettyacademy")
driver.find_element(By.CSS_SELECTOR, ".promoBtn").click()
Sleep()函数可以代替隐式等待正常工作。
任何人有相同的建议/理由。请指导
2条答案
按热度按时间6gpjuf901#
我认为这个问题是因为页面正在刷新,或者元素在被定位之后但在被单击之前从DOM中删除
为了解决这个问题我建议
并除去了
y1aodyip2#
搜索后需要等待一段时间: