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='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='images/201605/thumb_img/72_thumb_G_1462956048008.jpg']").click()
driver.find_element_by_xpath("//img[@src='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='themes/default/images/checkout.gif']").click()
#選擇城市 下拉框 1.兩次定位 2.xpath 定位層級標簽
province = driver.find_element_by_name("province")
province.find_element_by_xpath("//option[@value='5']").click()
city = driver.find_element_by_id("selCities_0")
city.find_element_by_xpath("//option[@value='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='5']").click()
driver.find_element_by_xpath("//*[@id='paymentTable']/tbody/tr[4]/td[1]/input").click()
driver.find_element_by_xpath("//input[@src='themes/default/images/bnt_subOrder.gif']").click()
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='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='images/201605/thumb_img/72_thumb_G_1462956048008.jpg']").click()
driver.find_element_by_xpath("//img[@src='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='themes/default/images/checkout.gif']").click()
#選擇城市 下拉框 1.兩次定位 2.xpath 定位層級標簽
province = driver.find_element_by_name("province")
province.find_element_by_xpath("//option[@value='5']").click()
city = driver.find_element_by_id("selCities_0")
city.find_element_by_xpath("//option[@value='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='5']").click()
driver.find_element_by_xpath("//*[@id='paymentTable']/tbody/tr[4]/td[1]/input").click()
driver.find_element_by_xpath("//input[@src='themes/default/images/bnt_subOrder.gif']").click()
轉載于:https://www.cnblogs.com/fbug/p/6128566.html
總結
以上是生活随笔為你收集整理的selenium自学笔记---ecshop购买脚本 xpath定位元素(下拉框,单选框)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用myeclipse创建带注解的mod
- 下一篇: PHP之factory