后浪小萌新Python --- selenium

tech2024-03-28  72

selenium的使用

selenium是自动化测试工具,可以通过代码控制浏览器获取网页数据。

基本使用

1.创建浏览器对象并且加载页面

from selenium import webdriver driver = webdriver.Chrome() driver.get('https://www.baidu.com')

2. 基本配置

from selenium import webdriver # 创建设置对象 options = webdriver.ChromeOptions() # 设置取消测试环境 options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置取消图片加载 options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) # 设置配置 driver = webdriver.Chrome(options=options)

3. 基本使用

from selenium import webdriver brower = webdriver.Chrome() try: brower.get('https://www.baidu.com') # 获取标签 input = brower.find_element_by_id('kw') # 操作标签 input.send_keys('Python') input.send_keys(Keys.ENTER) # 等待指定标签加载成功 wait = WebDriverWait(brower, 10) wait.until(EC.presence_of_element_located((By.ID, 'wrapper'))) # 获取相关信息 print(brower.current_url) print(brower.get_cookies()) print(brower.page_source) except: print('异常!') brower.close()

4. 创建浏览器对象

from selenium import webdriver brower = webdriver.Chrome() brower = webdriver.Firefox() brower = webdriver.Edge() brower = webdriver.PhantomJS() brower = webdriver.Safari()

5. 访问网页

from selenium import webdriver brower = webdriver.Chrome() brower.get('https://www.taobao.com') print(brower.page_source) brower.close()

6. 查找单个元素

from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.taobao.com') input_frist = browser.find_element_by_id('q') input_second = browser.find_element_by_css_selector('#q') input_third = browser.find_element_by_xpath('//*[@id="q"]') print(input_frist) print(input_second) print(input_third) browser.close()

7.查找多个元素

from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.taobao.com') lis = browser.find_elements_by_css_selector('.service-bd li') print(lis) browser.close()

8. 元素交互动作 - 对获取的元素调用交互方法

from selenium import webdriver import time browser = webdriver.Chrome() browser.get('https://www.taobao.com') input = browser.find_element_by_id('q') input.send_keys('iPhone') time.sleep(1) input.clear() input.send_keys('iPad') button = browser.find_element_by_class_name('btn-search') button.click()

9. 交互动作 - 将动作添加到动作链中串行执行

from selenium import webdriver from selenium.webdriver import ActionChains browser = webdriver.Chrome() url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get(url) browser.switch_to.frame('iframeResult') source = browser.find_element_by_css_selector('#draggable') target = browser.find_element_by_css_selector('#droppable') # 创建动作连对象 actions = ActionChains(browser) actions.drag_and_drop(source, target) actions.perform() # 开始执行动作链的动作

10. 执行JavaScript

from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.zhihu.com/explore') browser.execute_script('window.scrollTo(0, document.body.scrollHeight)') browser.execute_script('alert("To Bottom")')

11. 获取元素信息

# 获取属性 from selenium import webdriver from selenium.webdriver import ActionChains browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) logo = browser.find_element_by_id('special') print(logo) # 获取指定属性的值 print(logo.get_attribute('class')) # 获取文本值 from selenium import webdriver browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) input = browser.find_element_by_class_name('Button--blue') print(input.text) # 获取ID、位置、标签名、大小 from selenium import webdriver browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) input = browser.find_element_by_class_name('Button--blue') # 获取ID print(input.id) # 获取位置 print(input.location) # 标签名 print(input.tag_name) # 大小 print(input.size)

12. 等待

隐式等待 当使用了隐式等待执行测试的时候,如果webDriver没有在DOM中找到元素,将继续等待, 超出设定时间后则抛出找不到元素的异常,换句话说,当查找元素或元素并没有立即出现的时候,隐式等待 将等待一段时间再查找DOM,默认的时间是0 from selenium import webdriver browser = webdriver.Chrome() browser.implicitly_wait(10) # 等待10秒钟 url = 'https://www.zhihu.com/explore' browser.get(url) input = browser.find_element_by_class_name('zu-top-add-question') print(input) 显示等待 等到满足某个条件成立 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC browser = webdriver.Chrome() browser.get('https://www.taobao.com/') wait = WebDriverWait(browser, 10) # 等待指定元素加载成功 input = wait.until(EC.presence_of_element_located((By.ID, 'q'))) button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search'))) print(input, button)

10. 前进后退

import time from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.baidu.com/') browser.get('https://www.taobao.com/') browser.get('https://www.jd.com/') browser.back() time.sleep(1) browser.forward() browser.close()

11. Cookies

from selenium import webdriver browser = webdriver.Chrome() browser.get('https://maoyan.com/board/4?offset=0') # 获取cookie print(browser.get_cookies()) print() # 设置cookie browser.add_cookie({'name': '小明', 'domain': 'www.zhihu,com', 'value': 'germey'}) print(browser.get_cookies()) print() browser.delete_all_cookies() print(browser.get_cookies()) browser.close()

12. 选项卡

from selenium import webdriver import time browser = webdriver.Chrome() browser.get('https://www.baidu.com') browser.execute_script('window.open()') print(browser.window_handles) # browser.switch_to_window(browser.window_handles[1]) browser.switch_to.window(browser.window_handles[1]) browser.get('https://www.taobao.com') time.sleep(1) # browser.switch_to_window(browser.window_handles[0]) browser.switch_to.window(browser.window_handles[0]) browser.get('https://www.baidu.com')
最新回复(0)