selenium使用下拉框选项

from selenium import webdriver
import time

from selenium.webdriver.support.select import Select

driver = webdriver.Firefox(executable_path='/home/cwb/桌面/celery_used/for_test/driver/Firefox/geckodriver')
driver.get('http://192.168.1.106/index.php?m=user&c=public&a=login')
# 窗口最大化
driver.maximize_window()

driver.find_element_by_id('username').clear() # 清除输入框中的内容
driver.find_element_by_id('username').send_keys('cwb') # 在输入框中填入

driver.find_element_by_id('password').clear()
driver.find_element_by_id('password').send_keys('123456')

driver.find_element_by_class_name('login_btn.fl').submit()
time.sleep(10)
driver.find_element_by_xpath('//div[@class="w1100"]/div[1]/a/img').click()
driver.implicitly_wait(10) # 隐式等待(也就是在网页加载的时候等待,10秒,如果网页还未加载完成,直接报错)
driver.find_element_by_xpath('/html/body/div[5]/div/div[2]/div/a/img').click()

# 页面点击,跳转到新窗口
driver.switch_to.window(driver.window_handles[-1])

# 加入购物车
driver.find_element_by_id('joinCarButton').click()
# 去结算
driver.find_element_by_class_name('shopCar_T_span3').click()
driver.implicitly_wait(5)
driver.find_element_by_class_name('shopCar_btn_03.fl').click()
driver.implicitly_wait(5)

# 判断是否要添加收货地址
try:
    driver.find_element_by_class_name('add-address-1.selected')
except:
    driver.find_element_by_class_name('add-address').click()
    driver.find_element_by_class_name('add-new-name-span').send_keys('mark')
    driver.find_element_by_xpath('/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/div/div[2]/input').send_keys('15517875853')
    # 定位到对应的下拉框标签
    select = driver.find_element_by_class_name('add-new-area-select')
    # 通过Select对象来实现通过
    #显示文本(select_by_visible_text),下标(select_by_index),下拉选项对应的value值(select_by_value),等方法,
    Select(select).select_by_visible_text('北京市')

    select = driver.find_elements_by_class_name('add-new-area-select')
    Select(select[1]).select_by_visible_text('北京市')
    Select(select[2]).select_by_visible_text('东城区')
    driver.find_element_by_class_name('add-new-name-span-2').send_keys('supermarket')
    driver.find_element_by_class_name('add-new-name-span-3').send_keys('400590')
    driver.find_element_by_class_name('aui_state_highlight').click()

你可能感兴趣的:(selenium使用下拉框选项)