Reputation: 129
Her's what my code basically does(or i'm trying to do). Open a window, open a link from the page, fetch some data from the page and close the tab. The problem lies in closing the tab. Open the 2nd link again and perform the same operation again.
link.send_keys(Keys.CONTROL + 'w')
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 323, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 404, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 195, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 170, in check_response
raise exception_class(message, screen, stacktrace)
StaleElementReferenceException: Message: Element belongs to a different frame than the current one - switch to its containing frame to use it
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
from lxml import html
import requests
import xlwt
browser = webdriver.Firefox() # Get local session of firefox
# 0 wait until the pages are loaded
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it
browser.get("http://ae.bizdirlib.com/taxonomy/term/1493") # Load page
links = browser.find_elements_by_css_selector("h2 > a")
def test():#test function
elems = browser.find_elements_by_css_selector("div.content.clearfix > div > fieldset> div > ul > li > span")
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it
for elem in elems:
print elem.text
elem1 = browser.find_elements_by_css_selector("div.content.clearfix>div>fieldset>div>ul>li>a")
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it
for elems21 in elem1:
print elems21.text
return
for link in links:
link.send_keys(Keys.CONTROL + Keys.RETURN)
link.send_keys(Keys.CONTROL + Keys.PAGE_UP)
browser.switch_to_window(browser.window_handles[-1])
test() # Want to call test function
browser.implicitly_wait(3) # 3 secs should be enough. if not, increase it
# browser.quit()
browser.switch_to_window(browser.window_handles[0])
link.send_keys(Keys.CONTROL + 'w')
# browser.switch_to_window(browser.window_handles[0])
Upvotes: 2
Views: 300
Reputation: 12623
The switch_to_window
function is used when you working on multiple windows and not tabs. Hence, using that function is useless. As per this link, till date, Selenium officially has no support for tabs
When you execute link.send_keys(Keys.CONTROL + 'w')
notice that the link
element does not belong to the current tab which is displayed. Hence, you should select a random element from the current tab and then call send_keys
function.
Your for
should be like this:
for link in links:
link.send_keys(Keys.CONTROL + Keys.RETURN)
link.send_keys(Keys.CONTROL + Keys.PAGE_UP)
test()
#Here, 'r' is the random element
r = browser.find_element_by_css_selector("h2 > a")
r.send_keys(Keys.CONTROL + 'w')
Upvotes: 1