1 回答
TA贡献1851条经验 获得超4个赞
您必须按 class_name 搜索结果,而不是 id:
results = driver.find_elements_by_class_name("mapTrnSch")
所有其他代码都运行良好。
重要通知。你会有两个结果。第一个是表头,第二个是表内容。
这是我在没有 WebDriverWait 和 ActionChains 的情况下编写的示例:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = 'https://enquiry.indianrail.gov.in/ntes/trainOnMapBh.jsp'
driver = Firefox(firefox_binary=r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", options=opts)
driver.get(url)
time.sleep(5)
# Send search data
driver.find_element_by_id("trnSrchTxt").send_keys("56913") # Train
time.sleep(5)
driver.find_element_by_id("jrnyStn").send_keys('SBC') # Journey
time.sleep(5)
driver.find_element_by_id("searchTrainInMapBtn").click() # Submit button (seems like we do not need to click on it, but let's click for sure)
time.sleep(5)
# Gain results
results = driver.find_elements_by_class_name("mapTrnSch")
print(results[0].text) # 1st result for table headers
print(results[1].text) # 2st result for table content
添加回答
举报