Home > OS >  Send value of dictionary to text box - Python Selenium
Send value of dictionary to text box - Python Selenium

Time:03-09

I have a dictionary I am working with. The values of this dictionary have to then be sent using Selenium to a specific textbox in a website. I want to use a for loop so that it can iterate through the dictionary until finished.

Here is my sample code:

d = {'hello': 4.56, 'bye bye': 3.21}

driver = webdriver.Chrome(executable_path=r"C:\Users\Dtoro\AppData\Roaming\Python\Python310\chromedriver.exe")
Chrome = "C:\Program Files\Google\Chrome\Application\chrome.exe"
actions = ActionChains(driver)
driver.get("https://justnotepad.com/")

for key, value in d.items():
    driver.find_element(By.NAME, "editable_text").click()
    time.sleep(2)
    actions.send_keys(value)

In the above code, just 4.56 3.21 would be printed. I've tried doing print(value) and that didn't work. I also tried actions.send_keys(Keys.v) and that didn't work either.

Any help is greatly appreciated!

CodePudding user response:

You are only sending value, as per the code - actions.send_keys(value).

Try like this and confirm.

d = {'hello': 4.56, 'bye bye': 3.21}

driver.get("https://justnotepad.com/")

for key,value in d.items():
    textbox = driver.find_element(By.ID,"editable_text")
    textbox.send_keys(f"{key}:{value}\n") # Remove "\n" if the text should appear in a single line.
  • Related