美文网首页
Selenium基本用法汇总

Selenium基本用法汇总

作者: UnilinU | 来源:发表于2020-05-31 22:23 被阅读0次

    用法声明

    # coding=utf-8
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import TimeoutException
    import time
    
    url = 'http://www.baidu.com'
    ## firefox & chrome webdriver
    ## chrome webdriver: https://chromedriver.chromium.org/downloads 注意对应chrome版本,需随Chrome更新
    ## Firefox geckodriver.exe: https://github.com/mozilla/geckodriver/releases 需自行下载
    driver = webdriver.Firefox(firefox_binary=r'path\to\firefox.exe', \
        executable_path=r'path\to\geckodriver.exe')
    driver = webdriver.Chrome(executable_path, chrome_options = webdriver.ChromeOptions().add_argument("--incognito")) # 无痕窗口
    # driver.minimize_window()
    driver.get(url)
    

    三种等待方式

    ## https://huilansame.github.io/huilansame.github.io/archivers/sleep-implicitlywait-wait
    time.sleep(5)   # 强制等待:5s
    driver.implicitly_wait(5)   $ 隐式等待:5s内页面加载完成后执行后续动作
    WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, 'login-name')))  # 显示等待:5s内直至元素符合条件后执行定位
    

    几种元素定位方式

    ele = driver.find_element_by_xpath('//*[@id="username"]').send_keys(username) # 右键检查/F12 -> 定位标签 -> 右键copy -> copy xpath
    ele = driver.find_element_by_id('element_id')   # 注意AJAX下id可能会变动, 相应的xpath也可能变动
    ele = driver.find_element_by_css_selector('tag.class1.class2.class3')  # 灵活
    ele = driver.find_element_by_class_name('classname')    # 方便,但可能定位不唯一
    
    ele = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, 'login-name')))
    ele = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CLASS_NAME, 'classname')))
    ele = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="login_form"]/div/div[3]/button')))
    ele = WebDriverWait(driver, 5).until_not(EC.presence_of_element_located((By.CSS_SELECTOR, '.class.hide')))
    ## 异步加载元素定位可能一直为真,但也许是语义错误,此时需要time.sleep()
    

    滚动窗口

    https://blog.csdn.net/zwq912318834/article/details/79262007
    https://www.jianshu.com/p/3c6840ccf17d

    元素操作

    ele.click() # 按钮点击
    ele.sendkey('username') # 输入框传值
    ele.get_attribute('para_name') # 'para_name' in ('class', 'innerText', 'textContent', 'innerHTML') # 获取文本
    

    基本异常

    try:
        ele = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH , xpath)))
    except TimeoutException:
        print ("xpath: Loading took too much time!")
        pass # 忽略
    

    参考

    https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html
    https://stackoverflow.com/questions/49775502/webdriverwait-not-working-as-expected/49775808#49775808
    https://stackoverflow.com/questions/57262217/how-do-you-use-ec-presence-of-element-locatedby-id-mydynamicelement-excep

    相关文章

      网友评论

          本文标题:Selenium基本用法汇总

          本文链接:https://www.haomeiwen.com/subject/ciwrzhtx.html