python——selenium——xpath定位练习
1.以【百度】为例:使用selenium完成,进入http://news.baidu.com/,打印出【 热点要闻 】,点击【娱乐】,打印 【即时新闻】
(1)导入【webdriver】、【sleep】
复制代码
1
2
3from selenium import webdriver from time import sleep
(2)定义一个driver,使用火狐浏览器驱动,打开百度首页并最大化窗口
复制代码
1
2
3
4
5driver=webdriver.Firefox() driver.maximize_window() driver.get('http://news.baidu.com/') print('-------------------------↓【 热点要闻 】↓-----------------------------')
(3)打印出【 热点要闻 】
复制代码
1
2
3
4redianyaowen=driver.find_element_by_xpath('//*[@id="pane-news"]') print('热点要闻---->n',redianyaowen.text) print('----------------------------↑【 热点要闻 】↑--------------------------n')
(4)点击【娱乐】,打印 【即时新闻】
复制代码
1
2
3
4
5
6
7
8print('-------------------------↓【 即时新闻】↓-----------------------------') yule=driver.find_element_by_xpath('//*[@id="channel-all"]/div/ul/li[6]/a') yule.click() time.sleep(3) jishixinwen=driver.find_element_by_xpath('/html/body/div[3]/div[1]/div[2]/div/div[2]') print('娱乐---->即时新闻---->n',jishixinwen.text) print('----------------------------↑【即时新闻】↑--------------------------')
(5)运行结果:部分截图如下,成功打印
2.以【CRM悟空软件】为例:使用selenium Web UI 自动化测试完成登录和添加任务操作
(1)导入【webdriver】、【time】、【unittest】
复制代码
1
2
3
4import unittest from selenium import webdriver import time
(2)定义一个类名叫【MyTestCase】
复制代码
1
2class MyTestCase(unittest.TestCase):
(3)在这个类里面,在测试执行之前,定义一个方法名【setUpClass】,它相当于测试开始执行前的准备工作,包含了以下内容
复制代码
1
2
3
4
5
6@classmethod def setUpClass(cls) -> None: cls.URL = "http://192.168.10.188:8080/" cls.DRIVER = webdriver.Chrome() cls.DRIVER.maximize_window()
(4)定义第一个测试用例,命名为【test_1_login】,用于测试登录功能
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35def test_1_login(self): driver = self.DRIVER driver.get(self.URL) ele_username = driver.find_element_by_xpath("//input[@name='username']") ele_password = driver.find_element_by_xpath("//input[@name='password']") ele_login_btn = driver.find_element_by_xpath("//button[@class='el-button submit-btn el-button--default']") ele_username.clear() ele_username.send_keys("admin") ele_password.clear() ele_password.send_keys('******') ele_login_btn.click() time.sleep(3) #TODO 等待登录成功后,页面加载完成 success_url = driver.current_url print("登录成功后-URL->", success_url) self.assertTrue(success_url.endswith('workbench/index'), msg='错误原因:登录成功后,url没有改变') # self.assertEqual(True, False) # <a data-v-dd9a3ef0="" href="#/workbench/task" class="router-link-exact-active router-link-active"> try: ele_task_btn = driver.find_element_by_xpath("//a[@href='#/workbench/task']") except Exception as e: self.assertTrue(False, msg='错误原因:这个【任务】按钮不存在或找不到') else: ele_task_btn.click() if __name__ == '__main__': unittest.main()
(5)定义第二个测试用例,命名为【test_2_create_task】,用于测试添加任务功能
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28def test_2_create_task(self): time.sleep(3) driver = self.DRIVER ele_create_task_btn = driver.find_element_by_xpath("//button[@class='el-button new-btn el-button--primary']") ele_create_task_btn.click() time.sleep(3) TASK_NAME = "新建任务测试" ele_task_name = driver.find_element_by_xpath("//div[@class='el-input']/input") ele_task_name.send_keys(TASK_NAME) ele_task_detail = driver.find_element_by_xpath("//textarea[@class='el-textarea__inner']") ele_task_detail.send_keys("新任务描述") ele_save_btn = driver.find_element_by_xpath("//button[@class='el-button el-button--primary']") ele_save_btn.click() time.sleep(3) ele_list_box = driver.find_element_by_xpath("//div[@class='list-box']") ele_list_left = ele_list_box.find_element_by_xpath('div[1]/div[@class="list-left"]') print(ele_list_left.text) self.assertTrue( TASK_NAME in ele_list_left.text, msg=f'错误原因:创建成功后,第一条任务信息 不是 {TASK_NAME}') @classmethod def tearDownClass(cls) -> None: cls.DRIVER.quit() cls.URL = None if __name__ == '__main__': unittest.main()
(6)运行结果:成功进入【CRM悟空软件】首页,成功登录,成功添加新任务,图略。
最后
以上就是潇洒毛豆最近收集整理的关于python——selenium——xpath定位练习python——selenium——xpath定位练习的全部内容,更多相关python——selenium——xpath定位练习python——selenium——xpath定位练习内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复