0

I am new to Selenium and got stuck with the following problem.

I want to open this web site, fill out the form, click on Submit button and collect the value of Distance field in the response page.

This is my current code. I can fill out the form and click on Submit button. However, I don't know how to collect Distance value from the response page and save it in the list. I will need to execute this code in the loop. Therefore each response should be saved in a list.

Also I don't want to physically simulate the opening of a browser. Instead I want this process running in a background.

from   selenium import webdriver

browser = webdriver.Firefox()
browser.get("https://www.flightmanager.com/content/timedistanceform.aspx")

departure_airport = browser.find_element_by_id("ContentPlaceHolder1_txtDepartureICAO")
arrival_airport = browser.find_element_by_id("ContentPlaceHolder1_txtArrivalICAO")
submit   = browser.find_element_by_id("ContentPlaceHolder1_BtnSubmit")

departure_airport.send_keys("LEMD")
arrival_airport.send_keys("LEBL")

submit.click()

wait = WebDriverWait( browser, 5 ) 
ScalaBoy
  • 3,254
  • 13
  • 46
  • 84

1 Answers1

1

To loop, first you need to create list of dict like this

[
  {"departure" : "LEMD", "arrival" : "LEBL"}
  {"departure" : "AAAA", "arrival" : "BBBB"}
]

to select the distance from the following element

<td colspan="2" class="td4" align="left">
  Distance: <span class="td5">261.30 (NM) / 300.76 (MI) / 483.93 (KM)</span><br>
  Trip Time: <span class="td5">0:49 (includes 15 minute bias and air speed at 460Kts)</span><br>
</td>

use Selector

# CSS
td[colspan="2"] span
# or Xpath
//span[contains(text(), "KM")]

and complete code for loop

distance_calculator = [{"departure" : "LEMD", "arrival" : "LEBL"}]

for dc in distance_calculator:
    browser.get("https://www........com/timedistanceform.aspx")

    departure_airport = browser.find_element_by_id("ContentPlaceHolder1_txtDepartureICAO")
    arrival_airport = browser.find_element_by_id("ContentPlaceHolder1_txtArrivalICAO")
    submit   = browser.find_element_by_id("ContentPlaceHolder1_BtnSubmit")

    departure_airport.send_keys(dc["departure"])
    arrival_airport.send_keys(dc["arrival"])
    submit.click()

    distance = browser.find_element_by_css_selector('td[colspan="2"] .td5')
    #distance = browser.find_element_by_xpath('//span[contains(text(), "KM")]')

    dc["distance"] = distance.text

print(distance_calculator)

Results:

[
  {
    "departure": "LEMD",
    "arrival": "LEBL",
    "distance": "261.30 (NM) / 300.76 (MI) / 483.93 (KM)"
  },
  {
    .......
  }
]

For running in background or headless see this answer

ewwink
  • 18,382
  • 2
  • 44
  • 54