繁体   English   中英

Python:元素未附加到页面文档

[英]Python: element is not attached to the page document

我不断收到此错误,并且在为另一个元素修复它时,却无法为这个元素修复它。 我认为这是因为对于其他元素,我可以找到find_by_ID,而该元素没有ID。

while True:
try:
     driver.find_element_by_name('commit')
     break
except (NoSuchElementException, StaleElementReferenceException):
     time.sleep(1)
     wait=WebDriverWait(driver, 10,ignored_exceptions=ignored_exceptions).until(EC.presence_of_element_located((By.NAME, 'commit')))
driver.find_element_by_css_selector('input.button').click()

错误:

selenium.common.exceptions.StaleElementReferenceException:消息:陈旧元素引用:元素未附加到页面文档

参考页面: http : //www.supremenewyork.com/shop/shirts/go8jt7kse/f74y2ihpz

特定的HTML元素:

<input type="submit" name="commit" value="add to cart" class="button" />

首先,当您要对其执行操作的元素的属性已更改时,您将获得Stale Element Exception。

当从找到元素到执行操作之间存在时间差异时,可能会发生这种情况。

尝试使用Xpath而不是CSS

driver.findElement(By.Xpath("//input[.='add to cart']")).click();

让我知道是否有效

 try: driver.get("http://www.supremenewyork.com/shop/shirts/m2wvkj5u6") time.sleep(3) driver.find_element_by_css_selector("#add-remove-buttons > input").click() except: pass 

有用

 try: driver.get("http://www.supremenewyork.com/shop/shirts/m2wvkj5u6") time.sleep(3) driver.find_element_by_css_selector("#add-remove-buttons > input").click() except: pass 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM