Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my code

from selenium import webdriver

class Testone(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.PhantomJS()
        self.driver.cookiesEnabled = 'True'
        self.driver.set_window_size(1366,768)

    def test_url(self):
        max_wait = 10
        self.driver.get("http://ads.ibibo.com/afr.php?z=50005")
        self.driver.set_page_load_timeout(max_wait)
        self.assertIn("Advertisement", self.driver.title)
        self.driver.find_element_by_xpath("//img").click()
        #self.driver.find_element_by_css_selector("img").click()


    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main() 

I am getting this screen

Warning (from warnings module):
  File "C:\Python34\lib\site-packages\selenium\webdriver\phantomjs\service.py", line 76
    while not utils.is_connectable(self.port):
ResourceWarning: unclosed 
E
======================================================================
ERROR: test_url (__main__.Testone)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\siddharth.jain\Documents\Automation\test.py", line 28, in test_url
    self.driver.find_element_by_xpath("//div[@id='']/a/img").click()
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 230, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 662, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with xpath '//div[@id='']/a/img'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"103","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:57252","User-Agent":"Python-urllib/3.4"},"httpVersion":"1.1","method":"POST","post":"{\"value\": \"//div[@id='']/a/img\", \"using\": \"xpath\", \"sessionId\": \"da0e56b0-8996-11e4-96ef-e721b9f30c36\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/da0e56b0-8996-11e4-96ef-e721b9f30c36/element"}}
Screenshot: available via screen


----------------------------------------------------------------------
Ran 1 test in 3.075s

FAILED (errors=1)
share|improve this question
    
please provide the XML input as well –  Hans Z. Dec 22 '14 at 9:22

1 Answer 1

Waiting for an element explicitly made it work for me:

import unittest

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class Testone(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.PhantomJS()
        self.driver.cookiesEnabled = 'True'
        self.driver.set_window_size(1366,768)

    def test_url(self):
        max_wait = 10
        self.driver.get("http://ads.ibibo.com/afr.php?z=50005")
        self.driver.set_page_load_timeout(max_wait)
        self.assertIn("Advertisement", self.driver.title)

        element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "//img"))
        )
        element.click()


    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main() 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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