使用 Python 添加 text/date/time 到网络表单
Add text/date/time to webform with Python
我正在尝试使用 python 填写网络表单(请参阅附件代码)。
有四个字段......我设法填写了两个文本字段
"Start-Station" & "End-Station",一个日期字段和一个时间字段。
不幸的是,字段 3 和 4 没有填满我的输入数据。
我猜他们没有被正确识别....
有人知道我必须更改什么才能在那里添加数据吗?
感谢您的帮助。
from selenium import webdriver
driver = webdriver.Chrome('...path-to...\chromedriver.exe')
driver.get('https://www.bahn.de/p/view/index.shtml')
Checkdata = ['Start-Station', 'End-Station',
'Do, 12.03.20', '05:30']
for Check in Checkdata:
driver.find_element_by_id("js-auskunft-autocomplete-from").send_keys(Checkdata[0])
driver.find_element_by_id("js-auskunft-autocomplete-to").send_keys(Checkdata[1])
driver.find_element_by_id("dp1583859161773").send_keys(Checkdata[2])
driver.find_element_by_id("js-auskunft-timeinput").send_keys(Checkdata[3])
使用find_element_by_name
:
driver.find_element_by_id("js-auskunft-autocomplete-from").send_keys(Checkdata[0])
driver.find_element_by_id("js-auskunft-autocomplete-to").send_keys(Checkdata[1])
driver.find_element_by_name("date").send_keys(Checkdata[2])
driver.find_element_by_name("time").send_keys(Checkdata[3])
这是一个工作版本。
在日期和时间你必须清除输入。如果您按时清除输入,它将自动完成,因此您将使用 5 次退格键然后就可以了。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('https://www.bahn.de/p/view/index.shtml')
Checkdata = ['Start-Station', 'End-Station',
'Do, 12.03.20', '05:30']
driver.find_element_by_id("js-auskunft-autocomplete-from").send_keys(Checkdata[0])
driver.find_element_by_id("js-auskunft-autocomplete-to").send_keys(Checkdata[1])
driver.find_element_by_name("date").clear()
driver.find_element_by_name("date").send_keys(Checkdata[2])
for x in range(5):
driver.find_element_by_name("time").send_keys(Keys.BACKSPACE)
driver.find_element_by_name("time").send_keys(Checkdata[3])
附上我的工作解决方案,其中包含您的一些建议:
import selenium
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('path\to\.exe')
driver.get('https://www.bahn.de/p/view/index.shtml')
checkzeiten = ['start station', 'end statino','05:30']
# , ['2', 'München Hbf', 'Hochzoll Bahnhof, Augsburg (Bayern)',
# 'Mi, 11.03.20', '16:00']]
#for Check in CheckZeiten:
driver.find_element_by_id("js-auskunft-autocomplete-from").send_keys(checkzeiten[0])
driver.find_element_by_id("js-auskunft-autocomplete-to").send_keys(checkzeiten[1])
#Date field already contains todays date...that´s OK
for x in range(5): #delete old time from field & add new one
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[2]/div/div[4]/div/form/fieldset[1]/div[2]/div[2]/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[2]/div/div[4]/div/form/fieldset[1]/div[2]/div[2]/input').send_keys(checkzeiten[2])
#click SUBMIT button
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[2]/div/div[4]/div/form/fieldset[5]/div/input[1]').click()
我正在尝试使用 python 填写网络表单(请参阅附件代码)。 有四个字段......我设法填写了两个文本字段 "Start-Station" & "End-Station",一个日期字段和一个时间字段。 不幸的是,字段 3 和 4 没有填满我的输入数据。 我猜他们没有被正确识别.... 有人知道我必须更改什么才能在那里添加数据吗?
感谢您的帮助。
from selenium import webdriver
driver = webdriver.Chrome('...path-to...\chromedriver.exe')
driver.get('https://www.bahn.de/p/view/index.shtml')
Checkdata = ['Start-Station', 'End-Station',
'Do, 12.03.20', '05:30']
for Check in Checkdata:
driver.find_element_by_id("js-auskunft-autocomplete-from").send_keys(Checkdata[0])
driver.find_element_by_id("js-auskunft-autocomplete-to").send_keys(Checkdata[1])
driver.find_element_by_id("dp1583859161773").send_keys(Checkdata[2])
driver.find_element_by_id("js-auskunft-timeinput").send_keys(Checkdata[3])
使用find_element_by_name
:
driver.find_element_by_id("js-auskunft-autocomplete-from").send_keys(Checkdata[0])
driver.find_element_by_id("js-auskunft-autocomplete-to").send_keys(Checkdata[1])
driver.find_element_by_name("date").send_keys(Checkdata[2])
driver.find_element_by_name("time").send_keys(Checkdata[3])
这是一个工作版本。
在日期和时间你必须清除输入。如果您按时清除输入,它将自动完成,因此您将使用 5 次退格键然后就可以了。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('https://www.bahn.de/p/view/index.shtml')
Checkdata = ['Start-Station', 'End-Station',
'Do, 12.03.20', '05:30']
driver.find_element_by_id("js-auskunft-autocomplete-from").send_keys(Checkdata[0])
driver.find_element_by_id("js-auskunft-autocomplete-to").send_keys(Checkdata[1])
driver.find_element_by_name("date").clear()
driver.find_element_by_name("date").send_keys(Checkdata[2])
for x in range(5):
driver.find_element_by_name("time").send_keys(Keys.BACKSPACE)
driver.find_element_by_name("time").send_keys(Checkdata[3])
附上我的工作解决方案,其中包含您的一些建议:
import selenium
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('path\to\.exe')
driver.get('https://www.bahn.de/p/view/index.shtml')
checkzeiten = ['start station', 'end statino','05:30']
# , ['2', 'München Hbf', 'Hochzoll Bahnhof, Augsburg (Bayern)',
# 'Mi, 11.03.20', '16:00']]
#for Check in CheckZeiten:
driver.find_element_by_id("js-auskunft-autocomplete-from").send_keys(checkzeiten[0])
driver.find_element_by_id("js-auskunft-autocomplete-to").send_keys(checkzeiten[1])
#Date field already contains todays date...that´s OK
for x in range(5): #delete old time from field & add new one
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[2]/div/div[4]/div/form/fieldset[1]/div[2]/div[2]/input').send_keys(Keys.BACKSPACE)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[2]/div/div[4]/div/form/fieldset[1]/div[2]/div[2]/input').send_keys(checkzeiten[2])
#click SUBMIT button
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[2]/div/div[4]/div/form/fieldset[5]/div/input[1]').click()