0

I want to assert all ids in the list with for loop, my code is not working:

list = ["'block-views-news_of_the_day-block_1'", "'views_slideshow_singleframe_main_carousal_new_block-block_1-2'",
            "'block-views-recent_contents-block_1'","'block-views-recent_contents-block_2'","'block-views-home_page_events-block_1'",
            "'const-cont-home","block-block-13'", "'block-block-58'"]

for x in list:
self.assertTrue(self.driver.find_element_by_id(x))

Error

Traceback (most recent call last): File "C:\Users\xxxxx\xxxxx\Selenium Scripts\xxxr\TestCases.py", line 50, in test_flush_cache self.assertTrue(self.driver.find_element_by_id(x))
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 266, in find_element_by_id return self.find_element(by=By.ID, value=id_)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 744, in find_element {'using': by, 'value': value})['value']
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 233, in execute self.error_handler.check_response(response)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"'block-views-news_of_the_day-block_1'"}

  • where its get failed?Can you share error pls – NarendraC Aug 17 '16 at 10:02
  • Error updated above – Shehryar Khalid Aug 17 '16 at 10:15
  • Be careful of what assert are you are using, check what find_element_by_id returns, if returns true or object if the element is found. – lauda Aug 21 '16 at 15:44
1

Using single quotes inside of double quotes seems weird/wrong. So first thing would be to remove those.

Then if one of the elements from the list doest exist you will get a NoSuchElementException. If you want the test to fail instead of just having an exception in your script you should handle this exception. I would write this test like this:

from selenium.common.exceptions import NoSuchElementException

ids = [
    "block-views-news_of_the_day-block_1",
    "views_slideshow_singleframe_main_carousal_new_block-block_1-2",
    "block-views-recent_contents-block_1",
    "block-views-recent_contents-block_2",
    "block-views-home_page_events-block_1",
    "const-cont-home","block-block-13",
    "block-block-58"
]

for id in ids: 
    try:
        self.driver.find_element_by_id(id)
    except NoSuchElementException:
        self.fail()

In this case test will fail when one element is missing from the page and wont check for others. If you want to keep checking for other elements you need to have a list where you collect missing elements instead of self.fail().

0

Try to remove the double or single quotes, something like:

list = ['id1', 'id2', 'id3']

or

list = ["id1", "id2", "id3"]

As you see in the last line it includes the single quotes as part of the id and that results in an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.