.
搜索
相关推荐
热门标签

【Python入门教程】Selenium自学入门

时间:2020-01-16 09:30:59

selenium是在自动化测试中用的最多的一款工具之一,他能够为我们在web开发中带来许多便利。


假如页面中存在一个元素

kw = driver.find_elements_by_xpath("//input[@id='kw']").size

1.想要获取元素的长和宽,可以根据

kw.size['width']
kw.size['height']

2.想要获取元素在页面的位置
kw.location['x']
kw.location['y']

获取位置,并且使得该元素可见
kw.location_once_scrolled_into_view['x']
kw.location_once_scrolled_into_view['y']

3.想要在页面上执行JS代码,或者注入Js文件
js = '''
    var script = document.createElement('script');
    script.src = "https://www.xxx.com/my_init.js";
    document.getElementsByTagName('head')[0].appendChild(script);
'''
# 调用给搜索输入框标红js脚本
driver.execute_script(js)

4.想要在当前页面截图(图片会保存在脚本所在目录)
driver.save_screenshot('csdn.png')