1 回答

TA貢獻1834條經驗 獲得超8個贊
你得到('//*[@id="file-uploader"]')哪個是<a>標簽
但是你必須使用隱藏的<input type="file">(后面<a>)
import selenium.webdriver
your_file = "/home/you/file.doc"
your_email = "[email protected]"
url = 'https://www.wordtopdf.com/'
driver = selenium.webdriver.Firefox()
driver.get(url)
file_input = driver.find_element_by_xpath('//input[@type="file"]')
file_input.send_keys(your_file)
email_input = driver.find_element_by_xpath('//input[@name="email"]')
email_input.send_keys(your_email)
driver.find_element_by_id('convert_now').click()
使用 Firefox 66 / Linux Mint 19.1 / Python 3.7 / Selenium 3.141.0 測試
編輯:在zamzar.com 上上傳的相同方法
我第一次看到的情況(所以我花了更長的時間來創建解決方案):它<input type="file">隱藏在按鈕下,但不使用它來上傳文件。它動態創建第二個<input type="file">用于上傳文件(或者甚至可能是許多文件 - 我沒有測試它)。
import selenium.webdriver
from selenium.webdriver.support.ui import Select
import time
your_file = "/home/furas/Obrazy/37884728_1975437959135477_1313839270464585728_n.jpg"
#your_file = "/home/you/file.jpg"
output_format = 'png'
url = 'https://www.zamzar.com/'
driver = selenium.webdriver.Firefox()
driver.get(url)
#--- file ---
# it has to wait because paga has to create second `input[@type="file"]`
file_input = driver.find_elements_by_xpath('//input[@type="file"]')
while len(file_input) < 2:
print('len(file_input):', len(file_input))
time.sleep(0.5)
file_input = driver.find_elements_by_xpath('//input[@type="file"]')
file_input[1].send_keys(your_file)
#--- format ---
select_input = driver.find_element_by_id('convert-format')
select = Select(select_input)
select.select_by_visible_text(output_format)
#--- convert ---
driver.find_element_by_id('convert-button').click()
#--- download ---
time.sleep(5)
driver.find_elements_by_xpath('//td[@class="status last"]/a')[0].click()
- 1 回答
- 0 關注
- 289 瀏覽
添加回答
舉報