selenium核心api

66 阅读1分钟

网页超时

隐性等待

def test_implicitly_wait(self, browser):

隐性等待

browser.implicitly_wait(10)

browser.get("www.baidu.com") browser.find_element(By.XPATH, '//input[@id="kw"]').send_keys('明天再学') browser.find_element(By.ID, 'su').click() browser.find_element(By.LINK_TEXT, '知乎').click()

显性等待

def test_webdriverwait(self, browser): browser.get("www.baidu.com") browser.find_element(By.XPATH, '//input[@id="kw"]').send_keys('明天再学') browser.find_element(By.ID, 'su').click()

显性等待

locator = (By.LINK_TEXT, '知乎') condition = when.element_to_be_clickable(locator) WebDriverWait(browser, timeout=5).until(condition).click()

网页切换

窗口切换

def test_switch_window(page): page.get("www.baidu.com") page.find_element(By.XPATH, '//input[@id="kw"]').send_keys('明天再学') page.find_element(By.ID, 'su').click() #点击打开新窗口 page.find_element(By.LINK_TEXT, '知乎').click() #切换窗口 page.switch_to.window(page.window_handles[-1]) page.find_element(By.NAME, 'username')

iframe切换

def test_switch_iframe(page): page.get('v4.ketangpai.com/User/login.…') page.find_element(By.LINK_TEXT, '微信登录').click() #iframe切换 iframe = page.find_element(By.CSS_SELECTOR, '#login_container>iframe') page.switch_to.frame(iframe)

qrcode = page.find_element(By.CSS_SELECTOR, '.qrcode') print(qrcode.get_attribute('src'))

鼠标操作

悬停

def test_01(self, page): page.get('www.runoob.com/html/html-t…')

悬停

actions = ActionChains(page) menu = page.find_element(By.XPATH, "//a[contains(text(),'HTML / CSS')]") actions.move_to_element(menu).perform()

html_el = page.find_element(By.LINK_TEXT, "CSS 教程") html_el.click() time.sleep(3)

拖动

def test_drag_and_drop(page: Page): page.get('demos.telerik.com/kendo-ui/dr…') start_el = page.find_element(By.ID, "draggable") end_el = page.find_element(By.ID, "droptarget") actions = ActionChains(page) actions.drag_and_drop(start, end).perform()

示例2:

import time from selenium import webdriver from selenium.webdriver import ActionChains

driver = webdriver.Firefox() driver.implicitly_wait(10) driver.get('www.baidu.com') url = 'www.runoob.com/try/try.php…' driver.get(url) driver.switch_to.frame('iframeResult') start = driver.find_element('id', 'draggable') end = driver.find_element('id', 'droppable') ac = ActionChains(driver) ac.drag_and_drop(start, end).perform() time.sleep(2)

复制粘贴

def test_drag_and_drop(self,page): page.get('www.baidu.com') page.find_element(By.ID, 'kw').send_keys('明天再学')

actions = ActionChains(page) actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL) actions.perform()

actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL) actions.perform()

actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL) actions.perform()

actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL) actions.perform()

键盘快捷键

回车提交

el.send_keys("hello", Keys.ENTER)

组合键

url = 'www.baidu.com' browser = webdriver.Chrome(executable_path='chromedriver_95.exe') browser.implicitly_wait(5) browser.maximize_window() browser.get(url)

browser.find_element('id', 'kw').send_keys('困困')

actions = ActionChains(browser)

CTRL + A

actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

CTRL + C

actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()

CTRL + V

actions.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()

文件上传

import keyboard

keyboard.write('C:\Users\muji\Desktop\avatar.png') time.sleep(1) keyboard.press('enter')

窗口滚动

js = "Window.scrollTo(0, document.body.scrollHeight)" driver.execute_script(js)

js修改属性

el = driver.find_element("id", "id") js = 'arguments[0].value = "new"' driver.execute_script(js, el)