selenium 获取Dropbox的XPATH

pkln4tw6  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(186)

场景:我希望自动执行以下操作:“https://www.dummyticket.com/dummy-ticket-for-visa-application/“,这个页面,我能够与每个网页元素互动,除了某个DropBox,那里的XPATH对我来说很难找到,我已经尝试了很多方法(使用SelectorHub + Chropath),没有让我的代码与该项目互动。
我的要求很简单:我想与那个投件箱交互,并想选择“签证延期”。
请帮助我查找并指导找到正确的XPATH
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()

这我尝试过,但它不工作,请帮助我使用我的代码格式,谢谢

brvekthn

brvekthn1#

该下拉菜单是一个名为“选择”的特殊元素。
Selenium具有特殊功能,可通过索引可见文本选择此选择选项。
这里我使用了可视文本方法。
下面的代码是有效的:

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:\webdrivers\chromedriver.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')

结果为

相关问题