python 遍历select下拉框,获取option标签中value和text的值

  1. 在写12306爬虫时,遇到选择座席信息时,需要遍历select下拉框获取其中的座席信息。
    python 遍历select下拉框,获取option标签中value和text的值_第1张图片python 遍历select下拉框,获取option标签中value和text的值_第2张图片
  2. 代码如下:
     seat_types = self.driver.find_element_by_xpath('.//select[starts-with(@id,"seatType")]')
                        seat_ptions_list = seat_types.find_elements_by_tag_name("option")
                        for option in seat_ptions_list:
                            print("Value is: " + option.get_attribute("value"))
                            print("Text is: " + option.text.strip())
                            # if left_ticket in option.text:
                            if seat_info == option.get_attribute("value"):
                                option.click()
                            else:
                                print("席别信息不正确")

     

  3. 获取结果:

    python 遍历select下拉框,获取option标签中value和text的值_第3张图片

你可能感兴趣的:(Python爬虫)