Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
i have a selenium script with python which clicks a link and then switches to the tab the link has opened, this process needs to be repeated 3 times the first time it's fine i then do
driver.close()
to close the current active tab but in the second cycle i get this error:
File "main.py", line 50, in main
driver.find_element_by_xpath('//*[@id="daily-sets"]/mee-card-group[1]/div/mee-card[2]/div/card-content/mee-rewards-daily-set-item-content/div/div[3]/a').click()
File "C:\Users\\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
the code is this:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from data import *
def main():
driver = webdriver.Chrome(executable_path=driverpath, options=options)
driver.get(URL)
#first
driver.find_element_by_xpath('//*[@id="daily-sets"]/mee-card-group[1]/div/mee-card[1]/div/card-content/mee-rewards-daily-set-item-content/div/div[3]/a').click()
driver.switch_to.window(driver.window_handles[1])
time.sleep(30)
driver.close()
#second
time.sleep(0.5)
driver.find_element_by_xpath('//*[@id="daily-sets"]/mee-card-group[1]/div/mee-card[2]/div/card-content/mee-rewards-daily-set-item-content/div/div[3]/a').click()
driver.switch_to.window(driver.window_handles[1])
time.sleep(30)
driver.close()
#third
time.sleep(0.5)
driver.find_element_by_xpath('//*[@id="daily-sets"]/mee-card-group[1]/div/mee-card[3]/div/card-content/mee-rewards-daily-set-item-content/div/div[3]/a').click()
driver.switch_to.window(driver.window_handles[1])
time.sleep(30)
driver.close()
–
–
Here's an easier way to loop 3 times click and go from tab to parent handle.
for i in range(1,4):
driver.find_element_by_xpath('//*[@id="daily-sets"]/mee-card-group[1]/div/mee-card[{}]/div/card-content/mee-rewards-daily-set-item-content/div/div[3]/a'.format(i)).click()
driver.switch_to.window(driver.window_handles[-1])
time.sleep(30)
driver.close()
driver.switch_to.window(driver.window_handles[-1])
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.