如何获取python selenium中的所有数据?

如何获取python selenium中的所有数据?,python,selenium,xpath,Python,Selenium,Xpath,我无法从这个站点获取所有数据,它只是多次提供相同的数据 我尝试使用自定义xpath,但它多次提供单个数据 import time import selenium from selenium import webdriver browser = webdriver.Chrome() browser.get("https://www.spicejet.com/") departureButton = browser.find_element_by_id("ctl00_mainContent_d

我无法从这个站点获取所有数据,它只是多次提供相同的数据

我尝试使用自定义xpath,但它多次提供单个数据

import time
import selenium
from selenium import webdriver

browser = webdriver.Chrome()
browser.get("https://www.spicejet.com/")

departureButton = 
browser.find_element_by_id("ctl00_mainContent_ddl_originStation1_CTXT")

departureButton.click()
browser.find_element_by_partial_link_text("Kolkata").click()

arivalButton = browser.find_element_by_id("ctl00_mainContent_ddl_destinationStation1_CTXT")
arivalButton.click()

browser.find_element_by_partial_link_text("Goa").click()

date_position = 
browser.find_element_by_id("ctl00_mainContent_view_date1")
date_position.click()

search_date = "20-September 2019"
dep_date = search_date.split("-")

dep_month = dep_date[1]
dep_day = dep_date[0]

while browser.find_element_by_class_name("ui-datepicker-title").text !=  dep_month:
    browser.find_element_by_css_selector("a[data-handler='next']").click()

browser.find_element_by_xpath("//table//a[text()='"+dep_day+"']").click()
time.sleep(1)
pax_selct = browser.find_element_by_id("divpaxinfo").click()
time.sleep(.2)
# adult number
for i in range(0, 1 - 1):
    adults = browser.find_element_by_id("hrefIncAdt")
    adults.click()
# child number
for i in range(0, 1):
    childrens = browser.find_element_by_id("hrefIncChd")
    childrens.click()
# infant number
for i in range(0, 1):
    infants = browser.find_element_by_id("hrefIncInf")
    infants.click()

donebttn = browser.find_element_by_id("btnclosepaxoption").click()

searchBtn = 
browser.find_element_by_id("ctl00_mainContent_btn_FindFlights").click()

browser.switch_to.default_content()
flightarr = []
tbl_row = browser.find_elements_by_class_name("fare-row")
for item in tbl_row:
    if item.is_displayed():
        col = item
        flightinfo = {}
        flightNo = col.find_element_by_class_name("special").text
        depTime = col.find_element_by_class_name("departure-time").text
        trvlDuration = col.find_element_by_class_name("travel-duration").text
        arrv_time = col.find_element_by_class_name("hide-below-480").text
        price1 = item.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr/td[3]').text
        price2 = item.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr/td[4]').text
        price3 = item.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr/td[5]').text
        price4 = item.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr/td[6]').text


        flightinfo["flight_number"] = flightNo
        flightinfo["depart_time"] = depTime
        flightinfo["flight_duration"] = trvlDuration
        flightinfo["arrival_time"] = arrv_time
        flightinfo["i0"] = price1
        flightinfo["i1"] = price2
        flightinfo["i2"] = price3
        flightinfo["i3"] = price4

    flightarr.append(flightinfo)

print(flightarr)
time.sleep(2)
# browser.close()

必须通过在xpath中添加
,将for循环中的作用域限制为当前项。 下面是您应该使用的脚本

for item in tbl_row:
    if item.is_displayed():
        col = item
        flightinfo = {}
        flightNo = col.find_element_by_xpath(".//*[@class='special']").text
        depTime = col.find_element_by_xpath(".//*[@class='departure-time']").text
        trvlDuration = col.find_element_by_xpath(".//*[@class='travel-duration']").text 
        arrv_time = col.find_element_by_xpath(".//*[@class='hide-below-480']").text
        price1 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[3]').text
        price2 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[4]').text
        price3 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[5]').text
        price4 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[6]').text


        flightinfo["flight_number"] = flightNo
        flightinfo["depart_time"] = depTime
        flightinfo["flight_duration"] = trvlDuration
        flightinfo["arrival_time"] = arrv_time
        flightinfo["i0"] = price1
        flightinfo["i1"] = price2
        flightinfo["i2"] = price3
        flightinfo["i3"] = price4

    flightarr.append(flightinfo)

print(flightarr)

必须通过在xpath中添加
,将for循环中的作用域限制为当前项。 下面是您应该使用的脚本

for item in tbl_row:
    if item.is_displayed():
        col = item
        flightinfo = {}
        flightNo = col.find_element_by_xpath(".//*[@class='special']").text
        depTime = col.find_element_by_xpath(".//*[@class='departure-time']").text
        trvlDuration = col.find_element_by_xpath(".//*[@class='travel-duration']").text 
        arrv_time = col.find_element_by_xpath(".//*[@class='hide-below-480']").text
        price1 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[3]').text
        price2 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[4]').text
        price3 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[5]').text
        price4 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[6]').text


        flightinfo["flight_number"] = flightNo
        flightinfo["depart_time"] = depTime
        flightinfo["flight_duration"] = trvlDuration
        flightinfo["arrival_time"] = arrv_time
        flightinfo["i0"] = price1
        flightinfo["i1"] = price2
        flightinfo["i2"] = price3
        flightinfo["i3"] = price4

    flightarr.append(flightinfo)

print(flightarr)

不知道为什么,您正在对成人、儿童和婴儿使用for循环。我使用“for循环”来选择我需要的确切数字。我还可以使用什么?我使用了您的代码,但它工作不正常。它无法定位xpath。这是错误“selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:找不到元素:{“method”:“xpath”,“selector”:“/*[@id=“availabilitytabletable0”]/tbody/tr/td[3]”不确定原因,您正在对成人、儿童和婴儿使用for循环。我使用“for循环”来选择我需要的确切数字。我还可以使用什么?我使用了您的代码,但它工作不正常。它无法定位xpath。以下是错误“selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id=“availabilityTable0”]/tbody/tr/td[3]”