• 使用JavaScript操作页面元素


           在webdriver脚本代码中执行JavaScript代码,来实现对页面元素的操作。此种方式主要用于解决在某些情况下,页面元素的.click()方法无法生效等问题。

    #!usr/bin/env python  
    #-*- coding:utf-8 -*-  
    """ 
    @author:   sleeping_cat
    @Contact : zwy24zwy@163.com
    """ 
    #操作web页面的滚动条
    
    from selenium import webdriver
    import unittest
    import traceback
    import time
    
    class TestDemo(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
    
        def test_scroll(self):
            url = 'http://111.160.124.227:9194/'
            try:
                self.driver.get(url)
                self.driver.execute_script('window.scrollTo(100,document.body.scrollHeight);')
                #使用JavaScript的scrollTo函数和document.body.scrollHeight参数,将页面的滚动条滑动到页面的最下方
                time.sleep(2)
                self.driver.execute_script('document.getElementById("namekeyword").scrollIntoView(true);')
                #使用JavaScript的scrollIntoView函数将被遮挡的元素滚到可见屏幕上
                #scrollIntoView(true)将元素滚动到屏幕中间
                #scrollIntoView(false)将元素滚动到屏幕底部
                time.sleep(2)
                self.driver.execute_script('window.scrollBy(0,400);')
                #使用JavaScript的scrollBy方法,使用0和400横纵坐标参数
                time.sleep(2)
            except Exception as e:
                print(traceback.print_exc())
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
    #!usr/bin/env python  
    #-*- coding:utf-8 -*-  
    """ 
    @author:   sleeping_cat
    @Contact : zwy24zwy@163.com
    """ 
    #操作web页面的滚动条
    
    from selenium import webdriver
    import unittest
    import traceback
    import time
    
    class TestDemo(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
    
        def test_scroll(self):
            url = 'http://111.160.124.227:9194/'
            try:
                self.driver.get(url)
                self.driver.execute_script('window.scrollTo(100,document.body.scrollHeight);')
                time.sleep(2)
                self.driver.execute_script('document.getElementById("namekeyword").scrollIntoView(true);')
                time.sleep(2)
                self.driver.execute_script('window.scrollBy(0,400);')
                time.sleep(2)
            except Exception as e:
                print(traceback.print_exc())
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
    #!usr/bin/env python  
    #-*- coding:utf-8 -*-  
    """ 
    @author:   sleeping_cat
    @Contact : zwy24zwy@163.com 
    """ 
    #使用JavaScript操作页面元素
    
    from selenium import webdriver
    from selenium.common.exceptions import WebDriverException
    import unittest
    import traceback#导入堆栈类
    import time
    
    class TestDemo(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
    
    
        def test_executeScript(self):
            url = 'http://www.baidu.com'
            self.driver.get(url)
            searchInputBoxJS = 'document.getElementById("kw").value="光荣之路";'#构造JavaScript查找百度首页的搜索输入框的代码字符串
            searchButtonJS = 'document.getElementById("su").click()'#构造JavaScript查找百度首页的搜索按钮的代码字符串
            try:
                self.driver.execute_script(searchInputBoxJS)#通过JavaScript代码在百度首页搜索输入框中输入“光荣之路”
                time.sleep(2)
                self.driver.execute_script(searchButtonJS)#通过JavaScript代码单击百度首页上的搜索按钮
                time.sleep(2)
                self.assertTrue('百度百科' in self.driver.page_source)
            except WebDriverException as e:
                print("在页面中没有找到要操作的页面元素" ,traceback.print_exc())#打印异常的堆栈信息
            except AssertionError as e:
                print('页面不存在断言的关键字串')
            except Exception as e:
                print(traceback.print_exc())#发生其他异常时,打印异常堆栈信息
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    番茄工作法
    Linux命令学习chroot和chmode
    ejabberd与XMPP
    【转载】Understanding When to use RabbitMQ or Apache Kafka
    Activiti的ACT_GE_PROPERTY表初始化
    Spring transaction与EJB transaction的关系
    mysql & java & spring transaction isolation level
    Drools解决积分问题
    Rendertron:谷歌 Chrome 新的 headless 模式又贡献了一个新的技巧
    LDAP的前世今生
  • 原文地址:https://www.cnblogs.com/sleeping-cat/p/8117214.html
Copyright © 2020-2023  润新知