解决jenkins分辨率低获取不到元素的问题(Chrome浏览器,python+selenium+jenkins) - Go语言中文社区

解决jenkins分辨率低获取不到元素的问题(Chrome浏览器,python+selenium+jenkins)


解决jenkins分辨率低获取不到元素的问题(Chrome浏览器,python+selenium+jenkins)
问题描述:
自动化ui测试代码本地完全通过,但是部署在jenkins上就一直不通过的问题,通过在错误点截图了解到以为窗口未最大化,导致查了一些
最大化的资料,通过cheome的options参数最大化浏览器。但是始终没有解决,可以说后来经过搜索发现是因为jenkins分辨率低原因造成的。
查看分辨率的代码如下:

需要安装pywin32
import win32api
import win32con
print(win32api.GetSystemMetrics(win32con.SM_CXSCREEN))
print(win32api.GetSystemMetrics(win32con.SM_CYSCREEN))
后来想尽办法想要控制jenkins的分辨率但是始终没有解决,毕竟不是专门搞java的,jenkins那些东西着实无奈。
再后来想着改变字体的大小,但是结果不是太理想。
最终解决方案:
通过修改div的width,使页面元素全部暴露出来。网上找到一个动态修改html的方法,直接调用就可以了。
这里是我的代码里具体调用的部分:先获取需要扩大的div元素对象,然后调用JsOperate.py模块下的方法
main_content = Common().find_element(self.driver, ('id', 'main-content'))
JsOperate.addAttribute(self.driver, main_content, 'style', 'width:125%')
self.driver.get_screenshot_as_file('D:AUTO_YFDebugging_YF\b.png')
下面是封装的模块

JsOperate.py:

from selenium import webdriver
import unittest

def addAttribute(driver, elementobj, attributeName, value):
    '''
    封装向页面标签添加新属性的方法
    调用JS给页面标签添加新属性,arguments[0]~arguments[2]分别
    会用后面的element,attributeName和value参数进行替换
    添加新属性的JS代码语法为:element.attributeName=value
    比如input.name='test'
    '''
    driver.execute_script("arguments[0].%s=arguments[1]" % attributeName, elementobj, value)


def setAttribute(driver, elementobj, attributeName, value):
    '''
    封装设置页面对象的属性值的方法
    调用JS代码修改页面元素的属性值,arguments[0]~arguments[1]分别
    会用后面的element,attributeName和value参数进行替换
    '''
    driver.execute_script("arguments[0].setAttribute(arguments[1],arguments[2])", elementobj, attributeName, value)


def getAttribute(elementobj, attributeName):
    # 封装获取页面对象的属性值方法
    return elementobj.get_attribute(attributeName)


def removeAttribute(driver, elementobj, attributeName):
    '''
    封装删除页面属性的方法
    调用JS代码删除页面元素的指定的属性,arguments[0]~arguments[1]分别
    会用后面的element,attributeName参数进行替换
    '''
    driver.execute_script("arguments[0].removeAttribute(arguments[1])",
                          elementobj, attributeName)


class TestDemo(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_dataPicker(self):
        url = "D:PycharmProjectszouzoudom.html"
        self.driver.get(url)
        element = self.driver.find_element_by_xpath('//input')

        # 向页面文本框input标签中添加新属性name='search'
        addAttribute(self.driver, element, 'name', 'search')
        # 添加新属性后,查看一下新属性值
        print('添加的新属性值%s="%s"' % ("name", getAttribute(element, "name")))

        print('更改文本框中内容前的value的值:', getAttribute(element, 'value'))
        # 更改value的属性值为“这是更改后的值”
        setAttribute(self.driver, element, 'value', '这是更改后的值')
        print('更改后value的值为:', getAttribute(element, 'value'))

        # 查看更改前input页面元素中size属性值
        print('更改前size的属性值为:', getAttribute(element, 'size'))
        # 更改input的属性值为20
        setAttribute(self.driver, element, 'size', 20)
        print('更改后size的属性值为:', getAttribute(element, 'size'))

        # 查看删除input页面元素value属性前的值
        print('删除前文本框value的值:', getAttribute(element, 'value'))
        # 删除属性值
        removeAttribute(self.driver, element, 'value')
        print('删除后文本框value的值:', getAttribute(element, 'value'))


if __name__ == '__main__':
    unittest.main()

结果对比:

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_38648652/article/details/101760868
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 20:01:49
  • 阅读 ( 1179 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢