Open KeepInLove1006 opened 5 years ago
selenium-Webdriver 提供了强大的元素定位方法。
1、对于元素的属性包含class的情况适用
input_third = driver.find_element_by_class_name('s_ipt') input_third.click()
2、通用定位方式,对于不好定位的元素适用
button_product_1 = driver.find_element_by_css_selector("[class='inverted block-level primary button']").click()
3、对于元素的属性包含id的情况适用,推荐使用
input_first = driver.find_element_by_id("kw") input_first.click()
4、专门用于定位超链接的,也就是对应html页面中的标签,括号里传的值就是a标签中的超链接文字,两者的区别在于一个是完整的超链接文字,一个是可以只写部分超链接文字
driver.find_element_by_link_text("新闻").click()
driver.find_element_by_partial_link_text("闻").click()
5、仅仅通过标签名去定位时,一般一种标签在一个页面里面会出现不止一次甚至大量出现,这种定位方式的作用不是很大,所以用的也就比较少
input = driver.find_element_by_tag_name("input")
6、xpath是XML路径语言,它可以用来确定xml文档中的元素位置,通过元素的路径来完成对元素的查找。HTML就是XML的一种实现方式,所以xpath是一种非常强大的定位方式
# 一种是绝对路径定位 find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input")
# 还有一种是利用元素属性来进行xpath定位 find_element_by_xpath("//input[@id='kw']") find_element_by_xpath("//*[@name='wd']")
7、CSS属性定位可以比较灵活地选择控件的任意属性,定位方式也会比xpath快
# 如果有css基础的话就应该可以看懂,一般class是用.标记,id是用#标记,标签名直接写具体标签名就好了 driver.find_element_by_css_selector(".s_ipt").send_keys("selenium") driver.find_element_by_css_selector("#su").click()
# css定位里面也可以通过属性或者组合方式定位 driver.find_element_by_css_selector("input[autocomplete='off']").send_keys("Python") driver.find_element_by_css_selector("span.bg.s_btn_wr>input#su").click()
附上自己写的代码:
from selenium import webdriver import time # 下单流程 # 选择驱动 driver = webdriver.Chrome() # 定义url url = "https://xxxxxxxxxxxx.com/#/?_k=evmmbe" # 请求url driver.get(url) # 找到搜索框的元素 input_search1 = driver.find_element_by_class_name("input-field") # 点击搜索框元素进入搜索页面 input_search1.click() # 输入内容 input_search2 = driver.find_element_by_name("q") input_search2.send_keys("白鲸美甲") # 页面提交 input_search2.submit() # 找到作品 time.sleep(10) button_product = driver.find_element_by_css_selector("[class='product-image-box']") # print(button_product) button_product.click() # 点击购买 time.sleep(5) button_product_1 = driver.find_element_by_css_selector("[class='inverted block-level primary button']").click() # 弹出登录窗 time.sleep(5) input_phone = driver.find_element_by_css_selector("[class='phone-input']").send_keys("xxxxxxxxxxx") input_passwd = driver.find_element_by_css_selector("[class='pwd-input']").send_keys("a12345") # # input_passwd = driver.find_element_by_class_name("pwd-container") # # input_passwd.send_keys("xxxxxxxxxx") button_denglu = driver.find_element_by_css_selector("[class='login-btn block-level']").click() # button_denglu.click() # 提交订单页面 time.sleep(3) button_fukuan = driver.find_element_by_css_selector("[class='block-level inverted primary button']").click() # 付款页面 time.sleep(10) button_zhifu = driver.find_element_by_css_selector("[class='block-level primary inverted button']").click() # 去查看订单 time.sleep(5) button_dingdan = driver.find_element_by_css_selector("[class='pay-button']").click() # 关闭界面 driver.close()
selenium定位与查找元素
selenium-Webdriver 提供了强大的元素定位方法。
单个对象的定位方法
1、对于元素的属性包含class的情况适用
2、通用定位方式,对于不好定位的元素适用
3、对于元素的属性包含id的情况适用,推荐使用
4、专门用于定位超链接的,也就是对应html页面中的标签,括号里传的值就是a标签中的超链接文字,两者的区别在于一个是完整的超链接文字,一个是可以只写部分超链接文字
5、仅仅通过标签名去定位时,一般一种标签在一个页面里面会出现不止一次甚至大量出现,这种定位方式的作用不是很大,所以用的也就比较少
6、xpath是XML路径语言,它可以用来确定xml文档中的元素位置,通过元素的路径来完成对元素的查找。HTML就是XML的一种实现方式,所以xpath是一种非常强大的定位方式
7、CSS属性定位可以比较灵活地选择控件的任意属性,定位方式也会比xpath快
附上自己写的代码: