-1
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome(executable_path="C:/Users/PycharmProjects/seleniumproject/chromedriver")
driver.maximize_window()
body = {
  "AL": "Alabama",
  "AK": "Alaska",
  "AZ": "Arizona",
  "AR": "Arkansas",
  "CA": "California",
  "CO": "Colorado",
  "CT": "Connecticut",
  "DE": "Delaware",
  "DC": "District Of Columbia",
  "FL": "Florida",
  "GA": "Georgia",
  "HI": "Hawaii",
  "ID": "Idaho",
  "IL": "Illinois",
  "IN": "Indiana",
  "IA": "Iowa",
  "KS": "Kansas",
  "KY": "Kentucky",
  "LA": "Louisiana",
  "ME": "Maine",
  "MD": "Maryland",
  "MA": "Massachusetts",
  "MI": "Michigan",
  "MN": "Minnesota",
  "MS": "Mississippi",
  "MO": "Missouri",
  "MT": "Montana",
  "NE": "Nebraska",
  "NV": "Nevada",
  "NH": "New Hampshire",
  "NJ": "New Jersey",
  "NM": "New Mexico",
  "NY": "New York",
  "NC": "North Carolina",
  "ND": "North Dakota",
  "OH": "Ohio",
  "OK": "Oklahoma",
  "OR": "Oregon",
  "PA": "Pennsylvania",
  "RI": "Rhode Island",
  "SC": "South Carolina",
  "SD": "South Dakota",
  "TN": "Tennessee",
  "TX": "Texas",
  "UT": "Utah",
  "VT": "Vermont",
  "VA": "Virginia",
  "WA": "Washington",
  "WV": "West Virginia",
  "WI": "Wisconsin",
  "WY": "Wyoming"
}
driver.implicitly_wait(10)
driver.get("https://jwt.io/")
action = ActionChains(driver)
textbox = driver.find_element_by_xpath("//div[@class='js-payload']//div[@class='CodeMirror-code']")
textbox.click()
action.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform()
action.send_keys(Keys.BACK_SPACE).perform()
action.send_keys(body).perform()

time.sleep(5)
driver.quit()

Error:

  File "C:\Users\PycharmProjects\seleniumproject\testpractive\test2JWT.py", line 68, in <module>
    action.send_keys(body).perform()
  File "C:\Users\PycharmProjects\seleniumproject\venv\lib\site-packages\selenium\webdriver\common\action_chains.py", line 335, in send_keys
    typing = keys_to_typing(keys_to_send)
  File "C:\Users\PycharmProjects\seleniumproject\venv\lib\site-packages\selenium\webdriver\common\utils.py", line 151, in keys_to_typing
    typing.append(val[i])
KeyError: 0
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
SKK
  • 31
  • 1
  • 6
  • 1
    You want to pass the entire dict as string? – IoaTzimas Aug 29 '21 at 23:47
  • i am able to pass as a string, but it's entering each and every line, this is time taking, I want to reduce the time if I can directly set the value in the text box – SKK Aug 29 '21 at 23:50

1 Answers1

0

In automation you won't be able to, send_keys only accept strings not dict, or any other data structure. I even tried with pyperclip to copy the dict and past it into the send_keys. it did it line by line.

When I tried to parse entire dict. I got this :

raise PyperclipException('only str, int, float, and bool values can be copied to the clipboard, not %s' % (text.__class__.__name__))
pyperclip.PyperclipException: only str, int, float, and bool values can be copied to the clipboard, not dict

try this code with pyperclip :

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(50)
#driver.get("https://account.battle.net/creation/flow/creation-full")
#wait = WebDriverWait(driver, 20)

body = {
  "AL": "Alabama",
  "AK": "Alaska",
  "AZ": "Arizona",
  "AR": "Arkansas",
  "CA": "California",
  "CO": "Colorado",
  "CT": "Connecticut",
  "DE": "Delaware",
  "DC": "District Of Columbia",
  "FL": "Florida",
  "GA": "Georgia",
  "HI": "Hawaii",
  "ID": "Idaho",
  "IL": "Illinois",
  "IN": "Indiana",
  "IA": "Iowa",
  "KS": "Kansas",
  "KY": "Kentucky",
  "LA": "Louisiana",
  "ME": "Maine",
  "MD": "Maryland",
  "MA": "Massachusetts",
  "MI": "Michigan",
  "MN": "Minnesota",
  "MS": "Mississippi",
  "MO": "Missouri",
  "MT": "Montana",
  "NE": "Nebraska",
  "NV": "Nevada",
  "NH": "New Hampshire",
  "NJ": "New Jersey",
  "NM": "New Mexico",
  "NY": "New York",
  "NC": "North Carolina",
  "ND": "North Dakota",
  "OH": "Ohio",
  "OK": "Oklahoma",
  "OR": "Oregon",
  "PA": "Pennsylvania",
  "RI": "Rhode Island",
  "SC": "South Carolina",
  "SD": "South Dakota",
  "TN": "Tennessee",
  "TX": "Texas",
  "UT": "Utah",
  "VT": "Vermont",
  "VA": "Virginia",
  "WA": "Washington",
  "WV": "West Virginia",
  "WI": "Wisconsin",
  "WY": "Wyoming"
}

pyperclip.copy(str(body)) #Now you have it on your universal clipboard.
driver.implicitly_wait(10)
driver.get("https://jwt.io/")
action = ActionChains(driver)
textbox = driver.find_element_by_xpath("//div[@class='js-payload']//div[@class='CodeMirror-code']")
textbox.click()
action.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform()
action.send_keys(Keys.BACK_SPACE).perform()
action.send_keys(pyperclip.paste()).perform()

time.sleep(5)
driver.quit()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38