Apr-20-2023, 05:27 PM
To access an already open browser window, you can use the
Here's an updated version of your script that should work:
Let me know if you have any further questions or if there's anything else I can help with!
window_handles
property of the WebDriver
object, which returns a list of handles for all open windows. You can then switch to a specific window by passing the handle to the switch_to.window()
method.Here's an updated version of your script that should work:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.firefox.service import Service # path to keywords.txt-file keyword_file_path = r"D:\Dokumente\Scripte\keywords.txt" # starting Geckodriver-Service service = Service(".\geckodriver.exe") service.start() # initialise Firefox Webdriver driver = webdriver.Firefox(service=service) # switch to the first window driver.switch_to.window(driver.window_handles[0]) assert "Upload Page" in driver.title try: # find input field element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "tags")) ) # read keywords from file with open(keyword_file_path, "r") as f: keywords = [line.strip() for line in f] # insert keywords into input field followed by Enter for keyword in keywords: elem = driver.find_element(By.ID, "tags") elem.clear() elem.send_keys(keyword) elem.send_keys(Keys.RETURN) # wait until keyword is placed WebDriverWait(driver, 10).until( EC.text_to_be_present_in_element((By.ID, "tags"), keyword) ) finally: # quit Geckodriver-service driver.quit() service.stop()In this updated version, we first switch to the first window using
driver.switch_to.window(driver.window_handles[0])
. If you have multiple windows open and want to switch to a specific one, you can modify this line accordingly.Let me know if you have any further questions or if there's anything else I can help with!