python seleniumを用いてファイルのアップロードを試みています。
環境は以下の通りです。
windows10
chromedriver
python3.6.4
https://teratail.com/questions/133658
上記URLを参考にsend_keysでアップロードしたいファイルのパスとファイル名を送ろうと試みましたが、失敗します。下のスクリプトで要素の取得はできているよう(print(file_choice_str)までは回る)ですが、send_keysで停止(print("ok")までは回らない)します。
pyhton
1from selenium import webdriver 2from selenium.webdriver.common.by import By 3from selenium.webdriver.support.ui import WebDriverWait 4from selenium.webdriver.support import expected_conditions as EC 5from selenium.webdriver.common.keys import Keys 6from selenium.webdriver.chrome.options import Options 7import os 8 9driver = webdriver.Chrome("E:\chromedriver.exe") 10driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 11 12while True: 13 try: 14 element = driver.find_element_by_xpath("//*[@id=\"ImageUpload\"]/label") 15 dir_name = (os.path.dirname(__file__)) 16 file_choice_str = dir_name + '\test.jpg' 17 print(file_choice_str) 18 element.send_keys(file_choice_str) 19 print("ok") 20 break 21 except: 22 print("0",end="") 23
失敗する原因としてなにが考えられるでしょうか。
1.ファイルパスの書き方が違う
2.要素の取得方法が違う
等考え、パスを変えてみるなどして試してみましたが、うまくいきません。どのようにすればよいでしょうか。要因として他に考えられることでもいいのでお力添えください。
あなたの回答
tips
プレビュー