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设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

Pyinstaller 打包exe教程及问题解决

安装 pip insatll Pyinstaller 参数 pyinstaller -Fw main.py 参数 概述...

详解python编译器和解释器的区别

高级语言不能直接被机器所理解执行,所以都需要一个翻译的阶段,解释型语言用到的是解释器,编译型语言用到的是编译器。 编译型语言通常的执行过程是:源代码——预处理器——编译器——目标代码——...

python使用多进程的实例详解

python多线程适合IO密集型场景,而在CPU密集型场景,并不能充分利用多核CPU,而协程本质基于线程,同样不能充分发挥多核的优势。 针对计算密集型场景需要使用多进程,python的m...

Python实现字符串格式化的方法小结

Python2.6+ 增加了str.format函数,用来代替原有的'%'操作符。它使用比'%'更加直观、灵活。下面详细介绍一下它的使用方法。 下面是使用'%'的例子: "" "P...

python实现requests发送/上传多个文件的示例

1、需要的环境 Python2.X Requests 库 2、单字段发送单个文件 在requests中发送文件的接口只有一种,那就是使用requests.post的files参数, 请求...