首页 > 代码库 > selenium自学笔记---ecshop购买脚本 xpath定位元素(下拉框,单选框)

selenium自学笔记---ecshop购买脚本 xpath定位元素(下拉框,单选框)

本机环境:xamppv3.2.1+ecshop3.0  
1.元素定位写对,却一直报错,发现是页面元素加载的太慢,所以加上延时


from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.implicitly_wait(10)

#自动延时10s
driver.get("http://localhost/ecshop")
# driver.find_element_by_xpath(‘//div[@class="fl cart-yh"]/a‘).click()
driver.find_element_by_xpath("//a[@href=http://www.mamicode.com/‘user.php‘]").click()

# 登陆
driver.find_element_by_name("username").send_keys("zach")
driver.find_element_by_name(‘password‘).send_keys("123456")
driver.find_element_by_name("submit").click()
# 选择商品
time.sleep(3)
driver.find_element_by_xpath("//img[@src=http://www.mamicode.com/‘images/201605/thumb_img/72_thumb_G_1462956048008.jpg‘]").click()
driver.find_element_by_xpath("//img[@src=http://www.mamicode.com/‘themes/default/images/buybtn1.png‘]").click() #立即购买
# driver.find_element_by_xpath("/html/body/div[6]/div[1]/table/tbody/tr/td[2]/a/img").click() #结算
driver.find_element_by_xpath("//img[@src=http://www.mamicode.com/‘themes/default/images/checkout.gif‘]").click()

#选择城市 下拉框 1.两次定位 2.xpath 定位层级标签
province = driver.find_element_by_name("province")
province.find_element_by_xpath("//option[@value=http://www.mamicode.com/‘5‘]").click()
city = driver.find_element_by_id("selCities_0")
city.find_element_by_xpath("//option[@value=http://www.mamicode.com/‘50‘]").click()

driver.find_element_by_xpath("//select[@name=‘district‘]/option[3]").click()

driver.find_element_by_id("consignee_0").send_keys(‘张三‘)
driver.find_element_by_id("address_0").send_keys("深圳市宝安区")
driver.find_element_by_id("tel_0").send_keys("18888888888")
driver.find_element_by_class_name("bnt_blue_2").click()
#付款
time.sleep(3)
driver.find_element_by_xpath("//input[@name=‘shipping‘ and @value=http://www.mamicode.com/‘5‘]").click()

driver.find_element_by_xpath("//*[@id=‘paymentTable‘]/tbody/tr[4]/td[1]/input").click()
driver.find_element_by_xpath("//input[@src=http://www.mamicode.com/‘themes/default/images/bnt_subOrder.gif‘]").click()

selenium自学笔记---ecshop购买脚本 xpath定位元素(下拉框,单选框)