python-web根据元素属性进行定位的方法

yipeiwu_com6年前Python基础

1. 根据属性ID值进行定位

def test_find_element_by_id(self):
  # 定位搜索文本框
  search_input = self.driver.find_element_by_id("kw")
  # 输入关键字
  search_input.send_keys("马云")
  # 定位搜索按钮
  search_button = self.driver.find_element_by_id("su")
  # 点击搜索按钮
  search_button.click()
  # 喘口气
  time.sleep(2)
  # 断言结果
  actual_result = self.driver.page_source
  expect_result = "马云"
  self.assertIn(expect_result, actual_result)

2. 根据属性CLASS值进行定位

def test_find_element_by_class_name(self):
  # 定位搜索文本框
  search_input = self.driver.find_element_by_class_name("s_ipt")
  # 输入关键字
  search_input.send_keys("奥巴马")
  # 定位搜索按钮
  search_button = self.driver.find_element_by_id("su")
  # 点击搜索按钮
  search_button.click()
  # 喘口气
  time.sleep(2)
  # 断言结果
  actual_result = self.driver.page_source
  expect_result = "奥巴马"
  self.assertIn(expect_result, actual_result)

3. 根据属性NAME值进行定位

def test_find_element_by_name(self):
  # 定位搜索文本框
  search_input = self.driver.find_element_by_name("wd")
  # 输入关键字
  search_input.send_keys("特朗普")
  # 定位搜索按钮
  search_button = self.driver.find_element_by_id("su")
  # 点击搜索按钮
  search_button.click()
  # 喘口气
  time.sleep(2)
  # 断言结果
  actual_result = self.driver.page_source
  expect_result = "特朗普"
  self.assertIn(expect_result, actual_result)

4. 根据标签名称进行定位

5. 根据链接全部内容进行定位

6. 根据链接部分内容进行定位

def test_find_element_by_tag_name(self):
  # 定位搜索文本框
  search_input = self.driver.find_element_by_class_name("s_ipt")
  # 输入关键字
  search_input.send_keys("马化腾")
  # 定位搜索按钮
  search_button = self.driver.find_element_by_id("su")
  # 点击搜索按钮
  search_button.click()
  # 喘口气
  time.sleep(2)
  # 获取页面的返回结果
  # tag_names = self.driver.find_elements_by_tag_name("h3")
  # for tag_name in tag_names:
  #   print(tag_name.text)
  #   # 通过链接的文本信息进行定位
  #   link_text = self.driver.find_element_by_link_text(tag_name.text)
  #   # 对百度的结果依次进行点击
  #   link_text.click()
  # 根据部分链接文字进行定位
  pony_infos = self.driver.find_elements_by_partial_link_text("马化腾")
  for pony_info in pony_infos:
    # 依次打印每个元素的文本信息
    print(pony_info.text)
  # 断言结果
  actual_result = self.driver.page_source
  expect_result = "马化腾"
  self.assertIn(expect_result, actual_result)

7. 根据xpath进行定位

def test_find_element_by_xpath(self):
  # 找到搜索输入框
  # search_input = self.driver.find_element_by_xpath('/html/body/div[@id="wrapper"]/div[@id="head"]/div[@class="head_wrapper"]/div[@class="s_form"]/div[@class="s_form_wrapper soutu-env-nomac soutu-env-index"]/form[@class="fm"][@id="form"]/span[@class="bg s_ipt_wr quickdelete-wrap"]/input[@id="kw"][@class="a_ipt"]')
  search_input = self.driver.find_element_by_xpath('//*[@id="kw"]')
  # 输入关键字
  search_input.send_keys("天黑请闭眼")
  # 找到搜索按钮
  # search_button = self.driver.find_element_by_xpath('/html/body/div[@id="wrapper"]/div[@id="head"]/div[@class="head_wrapper"]/div[@class="s_form"]/div[@class="s_form_wrapper soutu-env-nomac soutu-env-index"]/form[@class="fm"][@id="form"]/span[@class="bg s_btn_wr"/input[@id="su"][@class="bg s_btn"]')
  search_button = self.driver.find_element_by_xpath('//*[@id="su"]')
  # 点击搜素按钮
  search_button.click()
  # 喘口气
  time.sleep(1)
  # 断言结果
  expect_value = "天黑请闭眼"
  actual_value = self.driver.page_source
  self.assertIn(expect_value,actual_value)

8. 根据css选择器进行定位

def test_find_element_by_css_selector(self):
  # search_input = self.driver.find_element_by_css_selector("#kw")
  search_input = self.driver.find_element_by_css_selector("input#kw")
  search_input.send_keys("狼人杀")
  search_button = self.driver.find_element_by_css_selector("input.bg.s_btn")
  search_button.click()
  # 喘口气
  time.sleep(1)
  # 断言结果
  expect_value = "狼人杀"
  actual_value = self.driver.page_source
  self.assertIn(expect_value, actual_value)

总结

以上所述是小编给大家介绍的python-web根据元素属性进行定位的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

Python自动化运维之Ansible定义主机与组规则操作详解

Python自动化运维之Ansible定义主机与组规则操作详解

本文实例讲述了Python自动化运维之Ansible定义主机与组规则操作。分享给大家供大家参考,具体如下: 一 点睛 Ansible通过定义好的主机与组规则(Inventory)对匹配的...

浅谈python中的变量默认是什么类型

1、type(变量名),输出的结果就是变量的类型; 例如 >>> type(6) <type 'int'> 2、在Python里面变量在声明时,不需要指定变...

对Python3+gdal 读取tiff格式数据的实例讲解

1、遇到的问题:numpy版本 im_data = dataset.ReadAsArray(0,0,im_width,im_height)#获取数据 这句报错 升级numpy:pip i...

解决Python下json.loads()中文字符出错的问题

解决Python下json.loads()中文字符出错的问题

Python:2.7 IDE:Pycharm5.0.3 今天遇到一个问题,就是在使用json.load()时,中文字符被转化为Unicode码的问题,解决方案找了半天,无解。全部代码...

在Python中调用Ping命令,批量IP的方法

如下所示: #!/usr/bin/env python #coding:UTF-8 ''''''' Author: jefferchen@163.com 可在命令行直接带目的IP...