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
#1
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()
Reply
#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


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