【selenium元素属性】获取元素属性

获取元素属性

1、获取窗口title
driver.title
2、获取url
driver.current_url
3、获取窗口截图
driver.get_screenshot_as_file('window.png')
4、获取元素截图
ele.screenshot('ele.png')
5、刷新页面
driver.refresh()
6、窗口最大化
driver.maximize_window()
7、窗口最小化
driver.minimize_window()
8、设置窗口大小(宽,高)
driver.set_window_size(450,890)
9、获取窗口尺寸
driver.get_window_size() ,返回类型为字典
10、获取元素尺寸
ele = driver.find_element_by_id('kw')
size = ele.size # 元素尺寸,返回类型为字典
11、获取窗口位置,相对屏幕的,窗口左上角的一个点
driver.get_window_position() {'x': 10, 'y': 10}
12、获取元素位置,相对窗口 
ele = driver.find_element_by_id('su')
e_position = ele.location
13、后退
driver.back()
14、前进
driver.forward()
15、获取元素源码html = driver.find_element_by_xpath("//*").get_attribute("outerHTML")
16、获取单个元素源码webElement.getAttribute("outerHTML")
17、获取单个元素的源码get_attribute('innerHTML')
18、获取页面源码driver.page_source
# 获取元素属性
       def get_element_attr(self, browser, ele, value, attr_value):
        try:
            if ele == 'title':
                title = browser.title
                return title
            elif ele == 'text':
                text = browser.find_element_by_xpath(value).text
                return text
            elif ele == 'tag':
                tag = browser.find_element_by_xpath(value).tag_name
                return tag
            elif ele == 'attr':
                attr = browser.find_element_by_xpath(value).get_attribute(attr_value)
                return attr
            elif ele == 'location':
                location = browser.find_element_by_xpath(value).location
                return location
            elif ele == 'id':
                id = browser.find_element_by_xpath(value).location
                return id
            elif ele == 'size':
                size = browser.find_element_by_xpath(value).size
                return size
            elif ele == 'url':
                url = browser.current_url
                return url
            elif ele == 'refresh':
                refresh = browser.refresh()
                return refresh
            elif ele == 'back':
                back = browser.back()
                return back
            elif ele == 'forward':
                forward = browser.forward()
                return forward
            elif ele == 'page_source':
                page_source = browser.page_source()
                return page_source

        except Exception as e:
            print("获取元素失败 %s" % e)

你可能感兴趣的:(Python,selenium,selenium,python)