selenium 基本用法

2019-09-29 16:57:19 浏览数 (1)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_40313634/article/details/96464341

  • selenium 基本用法举例
代码语言:javascript复制
#!/usr/bin/env python
import time

# 访问百度,隐形等待
def baidu():
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait

    url = 'https://www.baidu.com/'
    browser = webdriver.Chrome()
    browser.get(url)
    input = browser.find_element_by_id('kw')
    input = browser.find_element_by_class_name('s_ipt')
    input.send_keys('全球高武')
    input.send_keys(Keys.ENTER)
    wait = WebDriverWait(browser, 10)
    wait.until(EC.presence_of_element_located((By.ID, 'content_left')))
    print(browser.get_cookies())
    time.sleep(10)
    browser.close()


# 动作
def action():
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    import time

    browser = webdriver.Chrome()
    url = 'https://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('.ui-draggable')
    target = browser.find_element_by_css_selector('.ui-droppable')
    actions = ActionChains(browser)
    actions.drag_and_drop(source, target)
    actions.perform()
    time.sleep(10)

# 下拉进度条
def xia():
    from selenium import webdriver
    import time
    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")')
    time.sleep(10)

if __name__ == '__main__':
    xia()

0 人点赞