Mukesh
Mukesh

Reputation: 431

Fails my program after two iteration of loop at xpath using seelnium ide and python

After two iteration it fails and shows error.

If it happens because of not finding xpath by selenium ide then why it does not fail at second iteration of the loop.

How can I get output without any error and hit all 8 Urls one by one either xpath is available or not.

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#base Url
baseurl="http://www.incredibleindia.org"
driver = webdriver.Firefox()
driver.implicitly_wait(2)
driver.get(baseurl)

driver.implicitly_wait(2)
main_links_tabs=driver.find_elements_by_xpath("html/body/div[3]/div/div[1]/div[2]/ul/li/a")
all_tablength=len(main_links_tabs)
print all_tablength
main_link_list=[]
for i in range(all_tablength): 
    driver.implicitly_wait(3) 
    links=main_links_tabs[i].get_attribute('href')
    main_link_list.append(links) 
#all main_tab_link hit one by one  
for i in  main_link_list:
    print i
    driver.implicitly_wait(30) 
    driver.get(i) 

    #travel tabs data
    print "tabl links hit one by one"

    travel_tabs_sublinks=driver.find_elements_by_xpath(".//*[@id='left-inner-content']/div[2]/div/ul/li/a")

    travel_tabs_sublinks_len=len(travel_tabs_sublinks)
    print travel_tabs_sublinks_len

Output:

8
http://www.incredibleindia.org/en/travel
tabl links hit one by one
http://www.incredibleindia.org/en/trade
tabl links hit one by one
http://www.incredibleindia.org/en/#media
tabl links hit one by one
Traceback (most recent call last):
File "incredibleindia.py", line 27, in <module>
travel_tabs_sublinks=driver.find_elements_by_xpath(".//*[@id='left-        inner-content']/div[2]/div/ul/li/a")
File "/usr/local/lib/python2.7/dist-     packages/selenium/webdriver/remote/webdriver.py", line 244, in     find_elements_by_xpath
return self.find_elements(by=By.XPATH, value=xpath)
File "/usr/local/lib/python2.7/dist-  packages/selenium/webdriver/remote/webdriver.py", line 679, in find_elements
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.7/dist- packages/selenium/webdriver/remote/webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist- packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: The  given selector .//*[@id='left-inner-content']/div[2]/div/ul/li/a is either  invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath   expression .//*[@id='left-inner-content']/div[2]/div/ul/li/a because of  the following error:
TypeError: Argument 1 of Document.createNSResolver is not an object.
Stacktrace:
at FirefoxDriver.annotateInvalidSelectorError_ (file:///tmp/tmpDolyM9/extensions/[email protected]/components/driver-component.js:10245)
at FirefoxDriver.prototype.findElementsInternal_ (file:///tmp/tmpDolyM9/extensions/[email protected]/components/driver-component.js:10303)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpDolyM9/extensions/[email protected]/components/driver-component.js:603)

Upvotes: 1

Views: 1617

Answers (1)

alecxe
alecxe

Reputation: 474031

First of all, you are not using an implicitly_wait() correctly. It would not just sleep for N seconds, it would actually be executed immediately - it is saying the driver how much to wait every time it searches for an element:

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Instead, you need to use Explicit Waits. Here is the improved working version of the code:

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


url = "http://www.incredibleindia.org"
driver = webdriver.Firefox()
driver.get(url)

# wait for menu to being loaded
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.menu li > a")))

links = [a.get_attribute('href') for a in driver.find_elements_by_css_selector('div.menu li > a')]
for link in links:
    driver.get(link)

    # wait for menu to being loaded
    try:
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div#left-inner-content li > a")))
    except TimeoutException:
         print driver.title, "No sublinks"

    sublinks = driver.find_elements_by_css_selector("div#left-inner-content li > a")
    print driver.title, [sublink.text for sublink in sublinks]

Prints:

Incredible India - Travel [u'Rural Tourism', u'Mountain Trains & Luxury Trains', u'Eco Tourism', u'MICE', u'All Destinations']
Incredible India - Trade No sublinks
...

Upvotes: 2

Related Questions