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

yipeiwu_com7年前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实现简单名片管理系统

前言 之前看过一遍的python教程,真的是自己看过一遍,python的程序能看懂,但是很难去实现。比较困难的自己实现一些代码,找工作原因,自己又认认真真的看书,敲代码,后来看到了这个...

pytorch中的transforms模块实例详解

pytorch中的transforms模块中包含了很多种对图像数据进行变换的函数,这些都是在我们进行图像数据读入步骤中必不可少的,下面我们讲解几种最常用的函数,详细的内容还请参考pyto...

python调用并链接MATLAB脚本详解

发现一个很简单的配置方法,一直想写的没写上,今天抽空就把它给补充完整好了。 本文的配置方法Windows,Mac和Linux系统均适合。 一.安装MATLAB Engine API fo...

Pyqt5实现英文学习词典

Pyqt5实现英文学习词典

运用Python语言编写程序制作英文学习词典,词典有4个基本功能:添加、查询、删除和退出。程序读取源文件路径下的txt格式词典文件,若没有就创建一个。词典文件存储方式为“英文单词 中文单...

Python制作动态字符图的实例

Python制作动态字符图的实例

这次我们拿小龙猫来做演示 这里就不必多说了,也就导入几个用到的包: SOURCE_PATH:这个是GIF的路径OUTPUT_PATH:这个是每一帧的存放路径FRAMES_PATH:这...