Apr-09-2023, 10:55 AM
(This post was last modified: Apr-09-2023, 10:55 AM by Clixmaster.)
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 :)
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()
