Getting XPATH Of Dropbox

Question:

Scenario: I want to automate: "https://www.dummyticket.com/dummy-ticket-for-visa-application/" , this page, I am able to interact with every web-element except a certain DropBox, where the XPATH is very difficult for me to find, I have tried many ways (using SelectorHub + Chropath) nothing lets my code interact with that item.

My Requirement is simple: I want to interact with that dropbox, and want to select "Visa extension".

Please help me find & teach to find the right XPATH, I shall be very thankful.

enter image description here

import time
from telnetlib import EC

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()

driver.implicitly_wait(10)

driver.get("https://www.dummyticket.com/dummy-ticket-for-visa-application/")

driver.maximize_window()

driver.find_element(By.XPATH, "//input[@id=’product_550′]").click()

driver.find_element(By.XPATH, "//input[@placeholder=’first and middle name as on passport’]").send_keys("Waleed")
driver.find_element(By.XPATH, "//input[@placeholder=’last name as on passport’]").send_keys("Khan")

driver.find_element(By.XPATH, "//input[@id=’dob’]").click() # opens date picker

Date_Picker_month = Select(driver.find_element(By.XPATH, "//select[@aria-label=’Select month’]"))

Date_Picker_month.select_by_visible_text("Dec")

Date_Picker_year = Select(driver.find_element(By.XPATH, "//select[@aria-label=’Select year’]"))

Date_Picker_year.select_by_visible_text("1993")

all_dates = driver.find_elements(By.XPATH, "//table[@class=’ui-datepicker-calendar’]/tbody/tr/td/a")

for loop

for dates in all_dates:
if dates.text == "27":
dates.click()
break

driver.find_element(By.XPATH, "//div[1]//div[1]//form[1]//div[3]//div[1]//div[1]//p[4]//span[1]//input[1]").click()
driver.find_element(By.XPATH, "//div[1]//div[1]//form[1]//div[3]//div[1]//div[1]//p[4]//span[1]//input[1]").click()
driver.find_element(By.XPATH, "//input[@name=’fromcity’]").send_keys("Pakistan")
driver.find_element(By.XPATH, "//input[@name=’tocity’]").send_keys("United States Of America")

driver.find_element(By.XPATH, "//input[contains(@name,’departon’)]").click()
departure_month = Select(driver.find_element(By.XPATH, "//select[@class=’ui-datepicker-month’]"))

departure_month.select_by_visible_text("Dec")

departure_year = Select(driver.find_element(By.XPATH, "//select[@class=’ui-datepicker-year’]"))

departure_year.select_by_visible_text("2023")

all_dates1 = driver.find_elements(By.XPATH, "//table[@class=’ui-datepicker-calendar’]/tbody/tr/td/a")

for loop

for departure_dates in all_dates1:
if departure_dates.text == "11":
departure_dates.click()
break

ticketoption = driver.find_element(By.XPATH, "//span[@class=’selection’]")
ticketoption.find_element(By.XPATH, "//span[@title=’Visa extension’]").click()

This I tried but it’s not working, kindly help me using my format of code, thank-you

Asked By: Waleed Qamar

||

Answers:

That drop down is a special element called "Select".
Selenium has special feature to select this Select options by index, by value or by visible text.
Here I used the visible text approach.
The following code works:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)


url = "https://www.dummyticket.com/dummy-ticket-for-visa-application/"
driver.get(url)

select = Select(wait.until(EC.presence_of_element_located((By.ID, "reasondummy"))))
select.select_by_visible_text('Visa extension')

The result is
enter image description here

Answered By: Prophet