0
点赞
收藏
分享

微信扫一扫

selenium

心存浪漫 2022-04-25 阅读 107
selenium
from selenium import webdriver #导入selenium
huohu = webdriver.Firefox()    #打开浏览器
huohu.maximize_window()        #最大化
huohu.get('http://www.baidu.com')  #打开网址、项目
# huohu.get_window_size()  #获取浏览器尺寸
# huohu.set_window_size()  #设置浏览器尺寸
# huohu.get_window_position() #获取浏览器位置
# huohu.set_window_position(x,y) #设置浏览器位置
# 页面请求操作
# huohu.refresh()           #刷新页面操作
# huohu.back()          #回退到之前的页面
# huohu.forward()           #前进到之后的页面
# 3.获取断言信息
# 什么是断言?
# 断言是编程术语,表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真,可以在任何时候启用和禁用断言验证,因此可以在测试时启用断言而在部署时禁用断言。
# 获取断言信息的操作
# print(huohu.current_url)  #获取当前访问页面url
# print(huohu.title)        #获取当前浏览器标题
# 保存图片
# with open("1.png","wb") as f:
#     f.write(huohu.get_screenshot_as_png())
# print(huohu.page_source)   #查看网页源码
# 第一种定位方式
# huohu.find_element_by_id('wd')
#第二种定位方式
# huohu.find_elements_by_name("qwe")
# 第三种定位方式
# huohu.find_element_by_class_name("a")
# 第四种定位方式
# huohu.find_element_by_tag_name("vic")
# 第五种定位方式
# huohu.find_element_by_link_text("新闻")
# 第六种定位方式
# huohu.find_element_by_partial_link_text("新")
#第七种定位方式 xpath
# huohu.find_element_by_xpath("//*")
# 第八种定位方式 css
# huohu.find_elements_by_css_selector('#wd')
# 直接调用 id
# huohu.find_element("kw")
# 直接调用 name
# huohu.find_element("name")
# )获取所有窗口的句柄
# handles = driver. window_handles
# driver.switch_to_window(handles[n])
# 直接使用id值切换进表单,
# driver.switch_to.frame(value)
# 定位到表单元素,再切换进入
# el = driver.find_element_by_xxx(value)
# driver.switch_to.frame(el)
srk = huohu.find_element_by_css_selector("#kw")
srk.send_keys("10086")  #输入数据
bdyx = huohu.find_element_by_xpath("//*[@id='su']")
bdyx.click()  #点击
# bdyx.clear()  #清空
# huohu.quit()   #关闭所有标签/窗口
# huohu.close()  #关闭当前标签/窗口

from selenium import webdriver
import pytest,allure,os,time
class Testclass():
    @classmethod
    def setup_class(cls):  #开启
        cls.huohu = webdriver.Chrome('Chrome/Application/chromedriver.exe')
        cls.huohu.get("https://www.baidu.com/")
        aa = cls.huohu.find_element_by_xpath('//*[@id="s-top-left"]/a[1]')
        assert aa.text=="新闻"
    def test001(self):
        self.huohu.find_element_by_xpath('//*[@id="s-top-left"]/div/a').click()
        self.huohu.switch_to.window(self.huohu.window_handles[1])
        aa = self.huohu.find_element_by_xpath('//*[@id="head"]/div[2]/a[1]')
        assert aa.text=="百度首页"
    def test002(self):
        huohu = self.huohu
        huohu.find_element_by_xpath('//*[@id="content"]/div[15]/div[2]/a').click()
        huohu.switch_to.window(huohu.window_handles[2])
        aa = huohu.find_element_by_xpath('//*[@id="index-page-container"]/div[3]/div[1]/div[1]')
        assert aa.text=="日常快译"
    def test003(self):
        huohu = self.huohu
        huohu.find_element_by_xpath('//*[@id="whole-page-header"]/div/ul/li[2]/a').click()
    @classmethod
    def teardown_class(cls):
        time.sleep(10)
        cls.huohu.quit()   #关闭
if __name__ == '__main__':
    pytest.main(['tzlx.py'])

举报

相关推荐

0 条评论