从页面分离的Selenium Python错误列表

时间:2018-09-12 09:28:57

标签: python selenium

我正在尝试单击网络元素列表,但是当代码必须单击列表的第一个元素时,它将停止并显示错误

selenium.common.exceptions.StaleElementReferenceException: Message: stale 
element reference: element is not attached to the page document
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.38.552522 
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)

元素列表是这样的

<li>
  <a class="" data-images="   {&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/155274/ma/6800cDzhWOY.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/155274/zo/6800cDzhWOY.jpg&quot;}" data-style-name="Black" data-style-id="22859" data-sold-out="false" data-description="null" href="/shop/jackets/pnc61fwak/vswaf6ztx"><img width="32" height="32" src="//d17ol771963kd3.cloudfront.net/155274/sw/6800cDzhWOY.jpg" alt="6800cdzhwoy">
   </a>
  <a class="" data-images="{&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/155276/ma/fmx6wygm8ZU.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/155276/zo/fmx6wygm8ZU.jpg&quot;}" data-style-name="Black" data-style-id="22859" data-sold-out="false" data-description="null" href="/shop/jackets/pnc61fwak/vswaf6ztx?alt=0">
  <img width="32" height="32" src="//assets.supremenewyork.com/155276/sw/fmx6wygm8ZU.jpg" alt="Fmx6wygm8zu">
  </a>
</li>

<li>
  <a class="" data-images="   {&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/155274/ma/6800cDzhWOY.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/155274/zo/6800cDzhWOY.jpg&quot;}" data-style-name="Black" data-style-id="22859" data-sold-out="false" data-description="null" href="/shop/jackets/pnc61fwak/vswaf6ztx"><img width="32" height="32" src="//d17ol771963kd3.cloudfront.net/155274/sw/6800cDzhWOY.jpg" alt="6800cdzhwoy">
   </a>
  <a class="" data-images="{&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/155276/ma/fmx6wygm8ZU.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/155276/zo/fmx6wygm8ZU.jpg&quot;}" data-style-name="Black" data-style-id="22859" data-sold-out="false" data-description="null" href="/shop/jackets/pnc61fwak/vswaf6ztx?alt=0">
  <img width="32" height="32" src="//assets.supremenewyork.com/155276/sw/fmx6wygm8ZU.jpg" alt="Fmx6wygm8zu">
  </a>
</li>

<li>
  <a class="" data-images="   {&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/155274/ma/6800cDzhWOY.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/155274/zo/6800cDzhWOY.jpg&quot;}" data-style-name="Black" data-style-id="22859" data-sold-out="false" data-description="null" href="/shop/jackets/pnc61fwak/vswaf6ztx"><img width="32" height="32" src="//d17ol771963kd3.cloudfront.net/155274/sw/6800cDzhWOY.jpg" alt="6800cdzhwoy">
   </a>
  <a class="" data-images="{&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/155276/ma/fmx6wygm8ZU.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/155276/zo/fmx6wygm8ZU.jpg&quot;}" data-style-name="Black" data-style-id="22859" data-sold-out="false" data-description="null" href="/shop/jackets/pnc61fwak/vswaf6ztx?alt=0">
  <img width="32" height="32" src="//assets.supremenewyork.com/155276/sw/fmx6wygm8ZU.jpg" alt="Fmx6wygm8zu">
  </a>
</li>

<li>
  <a class="" data-images="   {&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/155274/ma/6800cDzhWOY.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/155274/zo/6800cDzhWOY.jpg&quot;}" data-style-name="Black" data-style-id="22859" data-sold-out="false" data-description="null" href="/shop/jackets/pnc61fwak/vswaf6ztx"><img width="32" height="32" src="//d17ol771963kd3.cloudfront.net/155274/sw/6800cDzhWOY.jpg" alt="6800cdzhwoy">
   </a>
  <a class="" data-images="{&quot;detail_url&quot;:&quot;//assets.supremenewyork.com/155276/ma/fmx6wygm8ZU.jpg&quot;,&quot;zoomed_url&quot;:&quot;//assets.supremenewyork.com/155276/zo/fmx6wygm8ZU.jpg&quot;}" data-style-name="Black" data-style-id="22859" data-sold-out="false" data-description="null" href="/shop/jackets/pnc61fwak/vswaf6ztx?alt=0">
  <img width="32" height="32" src="//assets.supremenewyork.com/155276/sw/fmx6wygm8ZU.jpg" alt="Fmx6wygm8zu">
  </a>
</li>

要点是,当单击列表的第一个元素时,其他元素仍保留在页面上,但代码似乎看不到。

我的代码

for colori in driver.find_elements_by_xpath(".//a[contains(@href,'shop/" + Accessorio + "')]"):

    pyautogui.moveTo(colori.location['x'] + 20, colori.location['y'] + 115, 1.0)
    pyautogui.click(colori.location['x'] + 20, colori.location['y'] + 115)

    time.sleep(1)
    colore = driver.find_element_by_css_selector(".style.protect")
    print(colore.text)
    print(Colore_item_supcommunity)
    if colore.text == Colore_item_supcommunity:
        a = +1
        break
    else:
        time.sleep(0.001)

另一个奇怪的事情是,当我使用Chrome默认配置文件运行代码时,会出现此错误。当我在没有Chrome默认配置文件的情况下运行代码时,它可以正常运行。 (我需要使用默认配置文件来运行它)

0 个答案:

没有答案