最近,我遇到了使网站自动化的Java代码,后来我改用python并执行了。
执行代码提交按钮时不起作用。谁能建议一种更好的方法来实现此代码?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://in.musafir.com/")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
departure = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='From']"))).send_keys("Kochi")
arrival = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='To']"))).send_keys("Mumbai")
driver.find_element_by_xpath("//input[@name='StartDate']").send_keys("21/08/19")
driver.find_element_by_xpath("//input[@name='EndDate']").send_keys("27/08/19")
s1 = Select(driver.find_element_by_xpath("//select[@name='AdultsFlight']"))
s1.select_by_index(2)
s1 = Select(driver.find_element_by_xpath("//select[@name='ChildrenFlight']"))
s1.select_by_index(1)
driver.find_element_by_xpath("//a[@class='optional submit button yellow flightsOnly_all buttonBound customButton']").click()
答案 0 :(得分:0)
您能否尝试使用按钮的显示名称的xpath
driver.find_element_by_xpath("//a[contains(text(),'Submit')]").click()
答案 1 :(得分:0)
您无法单击按钮,因为您的脚本无法正确填充出发地和目的地城市。
键入后,您需要单击输入下方的框中显示的自动建议结果之一:
因此,您需要添加另一个Explicit Wait,直到自动建议列表项可见,然后从列表中选择所需的城市。相关的XPath expression类似于:
//li[@class='airport active']/em
完整代码以防万一:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://in.musafir.com/")
wait = WebDriverWait(driver, 10)
departure = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='From']"))).send_keys("Kochi")
wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='airport active']/em"))).click()
arrival = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='To']"))).send_keys("Mumbai")
wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='airport active']/em"))).click()
driver.find_element_by_xpath("//input[@name='StartDate']").send_keys("21/08/19")
driver.find_element_by_xpath("//input[@name='EndDate']").send_keys("27/08/19")
s1 = Select(driver.find_element_by_xpath("//select[@name='AdultsFlight']"))
s1.select_by_index(2)
s1 = Select(driver.find_element_by_xpath("//select[@name='ChildrenFlight']"))
s1.select_by_index(1)
driver.find_element_by_xpath(
"//a[@class='optional submit button yellow flightsOnly_all buttonBound customButton']").click()
driver.quit()
答案 2 :(得分:0)
最终得到了解决方案,下面是代码。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://in.musafir.com/")
wait = WebDriverWait(driver, 10)
departure = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='From']"))).send_keys("Kochi")
wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='airport active']/em"))).click()
arrival = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='To']"))).send_keys("Mumbai")
wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@class='airport active']/em"))).click()
d1 = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='StartDate']"))).click()
d2 = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@date='2019-08-29']"))).click()
d1 = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='EndDate']"))).click()
d2 = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[@date='2019-08-30']"))).click()
s1 = Select(driver.find_element_by_xpath("//select[@name='AdultsFlight']"))
s1.select_by_index(2)
s1 = Select(driver.find_element_by_xpath("//select[@name='ChildrenFlight']"))
s1.select_by_index(1)
driver.find_element_by_xpath(
"//a[@class='optional submit button yellow flightsOnly_all buttonBound customButton']").click()
# driver.quit()