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 collections模块实例讲解

collections模块基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数...

十行代码使用Python写一个USB病毒

十行代码使用Python写一个USB病毒

大家好,我又回来了。 昨天在上厕所的时候突发奇想,当你把usb插进去的时候,能不能自动执行usb上的程序。查了一下,发现只有windows上可以,具体的大家也可以搜索(搜索关键词usb...

Python多层装饰器用法实例分析

本文实例讲述了Python多层装饰器用法。分享给大家供大家参考,具体如下: 前言 Python 的装饰器能够在不破坏函数原本结构的基础上,对函数的功能进行补充。当我们需要对一个函数补充不...

python计算两个数的百分比方法

工作中遇到了要计算两个数百分比的问题,python 2.7 环境。 代码: #!/usr/bin/env python #function: 计算百分比 #USAGE: python...

python实现二叉查找树实例代码

本文研究的主要是python实现二叉查找树的相关内容,具体介绍及实现如下。 1. 二叉查找树的定义: 左子树不为空的时候,左子树的结点值小于根节点,右子树不为空时,右子树的结点值大于根节...