2017-12-21 14 views
0

コードを実行しようとしているたびに、「StaleElementReferenceException」というエラーが発生し、それが私の人生を生きる地獄にしています。 DropDownAddressesファイルは、このようなようですSelenium + PythonのStaleElementReferenceException

import time 
import unittest 
import DropDownAddresses 
import re 


from selenium import webdriver 
from selenium.common.exceptions import * 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.by import By 
from selenium.webdriver.remote.webelement import WebElement 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.select import Select 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.common.exceptions import * 

UserEmail = "[email protected]" 
UserPassword = '12345678' 
SubcategoryName = input("Enter Name of Sub category: ") 


def dropDown(self, Category): 
    driver = self.driver 
    actions = ActionChains (driver) 
    actions.move_to_element (Category).perform () 
    SelectedCategory = driver.find_element_by_partial_link_text (SubcategoryName) 
    self.NameofSelectedCategory = print (SelectedCategory.text) 
    time.sleep (2) 
    actions.move_to_element (SelectedCategory).click ().perform () 
    time.sleep (2) 


def headings(self): 
    driver= self.driver 
    Pageheading = driver.find_element_by_tag_name ("h1").text 
    headingName = re.sub ("[^a-zA-Z]", "", Pageheading) 
    # Mainheading = int(Pageheading.text[:-4]) 
    offerCount = re.findall ('\d+', Pageheading) 

    NumberofOffers = (offerCount[0]) 
    if headingName == self.NameofSelectedCategory: 
     print ("Heading is correct i.e. " + headingName) 
    else: 

     print ("Heading was not correct") 

    if NumberofOffers == 1: 
     print ("Number of Offers in this section is " + str (NumberofOffers)) 
    else: 
     print ("Number of offers in this section are " + str (NumberofOffers)) 



class Categories(unittest.TestCase): 
    def setUp(self): 
     DropDownAddresses.browserCalling(self) 

    def test_Test1(self): 
     DropDownAddresses.openBrowser(self) 
     driver = self.driver 
     MoveSliderAhead = driver.find_element_by_xpath("//*[@id='wrapper']/div[2]/div[1]//div/button[2]") 
     MoveSliderAhead.click() 
     time.sleep(2) 
     print("Slider Moved Ahead") 

     MoveSliderBehind = driver.find_element_by_xpath("//*[@id='wrapper']/div[2]/div[1]//div/button[1]") 
     MoveSliderBehind.click() 
     time.sleep(2) 
     print("Slider Moved Behind") 


     HealthandBeautyDD = driver.find_element_by_xpath("//*[@id='bs-example-navbar-collapse-1']/ul/li[1]/a") 
     SportsnLeisureDD= driver.find_element_by_xpath("//*[@id='bs-example-navbar-collapse-1']/ul/li[2]/a") 
     FashionDD = driver.find_element_by_xpath("//*[@id='bs-example-navbar-collapse-1']/ul/li[3]/a") 
     JewellWatchDD = driver.find_element_by_xpath("//*[@id='bs-example-navbar-collapse-1']/ul/li[4]/a") 
     ElectronicsDD = driver.find_element_by_xpath("//*[@id='bs-example-navbar-collapse-1']/ul/li[5]/a") 


     dropDown(self, HealthandBeautyDD) 
     headings(self) 

     Wait = WebDriverWait (driver, 10, poll_frequency=1, 
           ignored_exceptions=[NoSuchElementException, ElementNotVisibleException]) 
     Wait.until (EC.element_to_be_clickable((By.XPATH, "//*[@id='bs-example-navbar-collapse-1']/ul/li[5]/a"))) 

     time.sleep(3) 
     dropDown(self, ElectronicsDD) 
     headings(self) 







    def tearDown(self): 
     print("Checking Categories") 

if __name__ == "__main__": 

に対し: コードは以下の通りである「健康と美容」のドロップダウンに

import os 
import time 
import unittest 


from selenium import webdriver 
from selenium.common.exceptions import * 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.select import Select 
from selenium.webdriver.support.ui import WebDriverWait 

UserEmail = "[email protected]" 
UserPassword = '12345678' 


def browserCalling(self): 
    driverlocation = "/Users/new/Documents/Learning/Drivers/selenium-2.53.1/py/selenium/webdriver/chromedriver" 
    os.environ["webdriver.chrome.driver"] = driverlocation 
    self.driver = webdriver.Chrome (driverlocation) 


def openBrowser(self): 
    driver = self.driver 
    url = "https://bizplace.theentertainerme.com/" 
    driver.get (url) 
    driver.implicitly_wait (10) 
    self.LoginButton = driver.find_element_by_id ("signin") 
    self.LoginButton.click () 
    self.LoginEmail = driver.find_element_by_xpath ("//*[@id='sign_form-default']//div[1]/input[@type='email']") 
    self.LoginEmail.clear () 
    self.LoginEmail.click () 
    self.LoginEmail.send_keys (UserEmail) 

    self.LoginPassword = driver.find_element_by_xpath (
     "//*[@id='sign_form-default']//div[1]/input[@type='password']") 
    self.LoginPassword.clear () 
    self.LoginPassword.click () 
    self.LoginPassword.send_keys (UserPassword) 

    self.LoginSubmitButton = driver.find_element_by_xpath ("//*[@id='sign_form-default']//div[3]/input") 
    self.LoginSubmitButton.click () 
    time.sleep (5) 

さて問題は、後に行われる工程、私はに切り替えたいです「エレクトロニクス」のドロップダウンが表示され、次のエラーが表示されます:

Error 
Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/case.py", line 58, in testPartExecutor 
    yield 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/case.py", line 600, in run 
    testMethod() 
    File "/Users/new/PycharmProjects/Selenium/Categories.py", line 87, in test_Test1 
    dropDown(self, ElectronicsDD) 
    File "/Users/new/PycharmProjects/Selenium/Categories.py", line 25, in dropDown 
    actions.move_to_element (Category).perform () 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/action_chains.py", line 83, in perform 
    action() 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/action_chains.py", line 293, in <lambda> 
    Command.MOVE_TO, {'element': to_element.id})) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute 
    self.error_handler.check_response(response) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document 
    (Session info: chrome=63.0.3239.84) 
    (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.13.0 x86_64) 




Ran 1 test in 47.314s 

FAILED (errors=1) 

Process finished with exit code 1 

誰かが私を助けてくれますか?

+0

[このarcticle](http://www.sahajamit.com/post/mystery-of-stale-element-reference-exception)が動作しているかどうかはわかりません。 /)あなたを助けることができます。 –

+0

http://www.seleniumhq.org/exceptions/stale_element_reference.jsp - 参考になります。 –

答えて

2

StaleElementReferenceExceptionは、あなたが要素を見つけたページから離れたために発生します。 例えば:

element = driver.find_element_by_css_selector('#menu') 
element.click() 
element.click() 

我々はStaleElementReferenceException

を得たが、場合:

element = driver.find_element_by_css_selector('#menu') 
element.click() 
element = driver.find_element_by_css_selector('#menu') 
element.click() 

すべてのものは、罰金になります。

テストを再設計する必要があります。

ElectronicsDD = driver.find_element_by_xpath("//*[@id='bs-example-navbar-collapse-1']/ul/li[5]/a") 
dropDown(self, ElectronicsDD) 
+0

それは働いた..ありがとうalotメイト:) – TeSter

関連する問題