Python Forum
Need help for script access via webdriver to an open web page in Firefox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help for script access via webdriver to an open web page in Firefox
#2
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!
Reply


Messages In This Thread
RE: Need help for script access via webdriver to an open web page in Firefox - by farshid - Apr-20-2023, 05:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  cant click button by script at page michael1834 1 1,215 Dec-08-2023, 04:44 PM
Last Post: SpongeB0B
  Problem with Selenium webdriver Fred 1 2,129 Jan-10-2022, 05:45 PM
Last Post: Larz60+
  Connect to existing Firefox session with Selenium euras 0 5,584 Feb-11-2021, 02:54 PM
Last Post: euras
  How to access a web service from a python script? dangermaus33 6 3,349 Dec-04-2020, 07:04 AM
Last Post: dangermaus33
  Can't open Amazon page Pavel_47 3 3,328 Oct-21-2020, 09:13 AM
Last Post: Aspire2Inspire
  Which webdriver is required for selenium in Pydroid App Rahatt 1 6,489 Jul-31-2020, 01:39 AM
Last Post: Larz60+
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,938 Jun-07-2020, 04:42 PM
Last Post: samlee916
  Hyperlink Click is not working in Selenium webdriver rajeev1729 0 2,081 May-02-2020, 11:21 AM
Last Post: rajeev1729
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 3,704 Mar-19-2020, 06:13 PM
Last Post: apollo
  Selenium webdriver error WiPi 4 12,375 Feb-09-2020, 11:38 AM
Last Post: WiPi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020