Need help for script access via webdriver to an open web page in Firefox - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Need help for script access via webdriver to an open web page in Firefox (/thread-39756.html) |
Need help for script access via webdriver to an open web page in Firefox - Clixmaster - Apr-09-2023 Hi@all, I have the following task: In a form in a LogIn area, keywords are to be entered one after the other into an input field with the ID "tags", each of which is to be confirmed individually with ENTER. This is extremely time-consuming manually, especially since the keywords are already available as a list. Intended solution: ... is a Python script that is started by key combination when the web page is open, reads in the keywords line by line from a given text file, identifies the correct browser tab and inserts each keyword there followed by an Enter. Development status: I have only been working with Python for 2 days and have tried to write a working script using ChatGPT. Unfortunately without success so far. The following script opens a new window itself instead of accessing the open window. I obviously lack fundamental knowledge here. I would be very grateful if you could help me a little bit. I have to recover a little from ChatGPT first :) 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) # get access to already opened window driver.switch_to.window(window_handle) 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() RE: Need help for script access via webdriver to an open web page in Firefox - farshid - Apr-20-2023 To access an already open browser window, you can use the 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! |