実現したいこと
言語はpythonを使っています。
excleからデータを取得して、chromedriverでブラウザに入力したいです。
excleの中身はstr型、int型、float型と混在しております。
列ごとに新規ページを作成し、行ごとにブラウザの入力欄を埋めていくのですが、
float型でエラーが発生します。
エラーを読むとwebにタイピングする際に文字数をlenで数えているようなのですが、回避方法がまったくわかりません。
float型を変える、send_keysの違うやり方があるなど、ご教授願います。
発生している問題・エラーメッセージ
File "site-packages\selenium\webdriver\common\utils.py", line 150, in keys_to_typing object of type 'float' has no len()
該当のソースコード
python
1import time 2import openpyxl 3import tkinter as tk 4import os 5import pprint 6import sys 7import signal 8 9from selenium import webdriver 10from selenium.webdriver.common.by import By 11from selenium.webdriver.support.ui import WebDriverWait 12from selenium.webdriver.support import expected_conditions as EC 13from selenium.common.exceptions import TimeoutException 14from tkinter import ttk 15from tkinter import filedialog 16from tkinter import messagebox 17 18#from selenium.webdriver.Chrome.options import Options 19 20def fselenium(root): 21 22 #excelファイルの取り込み 23 #ユーザーパス 24 UserPath = os.path.join(os.path.expanduser('~'),"Desktop","テンプレ.xlsx") 25 if os.path.exists(UserPath): 26 book = openpyxl.load_workbook(UserPath) 27 sheet = book["Sheet1"] 28 cells = sheet.values 29 else: 30 var_err.set(UserPath + 'が見つかりません。\nファイルを確認して下さい。') 31 sys.exit 32 33 itemList = list(cells) 34 35 if len(itemList) < 2: 36 var_err.set("ファイルの中身がありません。\nファイルを確認して下さい。") 37 sys.exit 38 39 driver_path = "C:/driver/chromedriver.exe" 40 41 driver = webdriver.Chrome(driver_path) 42 url = '入力サイト' 43 driver.get(url) 44 driver.implicitly_wait(10) ←信じれてない 45 elem_search_btn = driver.find_element_by_xpath("/html/body/nav/ul/li[2]/a") 46 elem_search_btn.click() 47 48 WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/button[2]"))) 49 time.sleep(3) #予備スリープ 50 elem_search_btn = driver.find_element_by_xpath("/html/body/div[1]/div/button[2]") 51 elem_search_btn.click() 52 time.sleep(3) #予備スリープ 53 54 id = TextBox1.get() 55 pass = TextBox2.get() 56 57 WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@id='email']"))) 58 elem_search_word = driver.find_element_by_xpath("//*[@id='email']") 59 elem_search_word.send_keys(id) 60 elem_search_word = driver.find_element_by_xpath("//*[@id='pass']") 61 elem_search_word.send_keys(pass) 62 elem_search_btn = driver.find_element_by_xpath("//*[@id='u_0_0']") 63 elem_search_btn.click() 64 time.sleep(5) #予備スリープ 65 66 driver.switch_to.window(handle_array[0]) 67 time.sleep(3) #予備スリープ 68 69 startNum = int(TextBox3.get()) 70 endNum = int(TextBox4.get()) 71 loopNum = endNum - startNum + 1 72 #登録する内容のリストitemList 73 for i in range(loopNum): 74 try: 75 #新規作成 76 elem_search_btn = driver.find_element_by_xpath("/html/body/div[1]/div/button") 77 elem_search_btn.click() 78 79 WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@id='inputKwAll']"))) 80 time.sleep(3) 81 elem_search_word = driver.find_element_by_xpath("//*[@id='inputKwAll']") 82 elem_search_word.send_keys(itemList[i + startNum][0]) 83 for j in range(10): 84 if itemList[i + startNum][j + 3] is not None: 85 elem_search_word = driver.find_element_by_xpath("//*[@id='inputKwe" + str(j + 1) + "']") 86 elem_search_word.send_keys(itemList[i + startNum][j + 3]) #バグ発生部分 87 else : 88 break 89 90 elem_search_btn = driver.find_element_by_xpath("/html/body/div[1]/div/form/button") 91 elem_search_btn.click() 92 # ページ上のすべての要素が読み込まれるまで待機(30秒でタイムアウト判定) 93 WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/table/tbody/tr[2]/td[3]/button"))) 94 time.sleep(3) 95 96 elem_search_btn = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/button") 97 elem_search_btn.click() 98 WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/table/tbody/tr[1]/td[4]/button[1]"))) 99 time.sleep(3) 100 except TimeoutException: 101 elem_search_btn = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/button") 102 elem_search_btn.click() 103 WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/table/tbody/tr[1]/td[4]/button[1]"))) 104 time.sleep(3) 105 next 106 driver.quit()
試したこと
map関数で全部str化しようとしましたが、思うような結果にできませんでした。
python
1itemMap = [*map(lambda x: str(x),itemList)]
また、一つ一つのtypeを見て条件でわけようとしましたが、うまく動作せず、ものすごい数の複製リストが生まれただけでした。
python
1row=[] 2new_itemList=[] 3for i in range(len(itemList)): 4 for j in range(10): 5 if type(itemList) is str: 6 row.append(itemList[i][j+3]) 7 if type(itemList) is not str: 8 row.append(str(itemList[i][j+3])) 9 new_itemList.append(row)
拙い文章で伝わりにくいかもしれませんが、よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/15 04:51