selenium

selenium和phantomJS&headless

浏览器驱动下载

IE11的Webdriver下载:
    http://dl.pconline.com.cn/download/771640-1.html
    链接:https://pan.baidu.com/s/13TTyXGNaG5cpSNdl1k9ksQ 密码:2n9n

Chrome65.0.3325.146的webdriver驱动下载:
    多版本:http://chromedriver.storage.googleapis.com/index.html
    或 http://npm.taobao.org/mirrors/chromedriver/2.43/

Firefox58的webdriver驱动下载
    链接:https://pan.baidu.com/s/1RATs8y-9Vige0IxcKdn83w 密码:l41g

selenium使用

get(url):打开URL

def openURL():
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    print(driver.page_source)

clear() : 清除数据 Clears the text if it’s a text entry element.

page_source:获取HTML源码

close():关闭

quit():全部关闭

click():点击,Clicks the element.

execute_script(script, *args): 执行脚本

driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")

# 下拉滚动条,使浏览器加载出动态加载的内容
while True:
    # 可能像这样要拉很多次,中间要适当的延时。
    # 如果说说内容都很长,就增大下拉的长度。
    for i in range(10):
        driver.execute_script("window.scrollBy(0,1000)")
        time.sleep(3)
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    break

查找元素

find_element(by='id', value=None)

find_element_by_class_name(name)

Finds element within this element’s children by class name.

find_element_by_css_selector(css_selector)

Finds element within this element’s children by CSS selector.

find_element_by_id(id_)

Finds element within this element’s children by ID.

find_element_by_link_text(link_text)

Finds element within this element’s children by visible link text.

find_element_by_name(name)

Finds element within this element’s children by name.

find_element_by_tag_name(name)

Finds element within this element’s children by tag name.

find_element_by_xpath(xpath)

Finds element by xpath.

myelement.find_element_by_xpath(".//a")

However, this will select the first link on the page.

myelement.find_element_by_xpath("//a")

find_elements(by='id', value=None)

‘Private’ method used by the find_elements_by_* methods.

find_elements_by_class_name(name)

Finds a list of elements within this element’s children by class name.

find_elements_by_css_selector(css_selector)

Finds a list of elements within this element’s children by CSS selector.

find_elements_by_id(id_)

Finds a list of elements within this element’s children by ID. Will return a list of webelements if found, or an empty list if not.

find_elements_by_link_text(link_text)

Finds a list of elements within this element’s children by visible link text.

find_elements_by_name(name)

Finds a list of elements within this element’s children by name.

find_elements_by_tag_name(name)

Finds a list of elements within this element’s children by tag name.

find_elements_by_xpath(xpath)

Finds elements within the element by xpath.

get_attribute(name)

Gets the given attribute or property of the element.

示例:

# Check if the "active" CSS class is applied to an element.
is_active = "active" in target_element.get_attribute("class")

save_screenshot(filename)

Saves a screenshot of the current element to a PNG image file. Returns

send_keys(*value)

Simulates typing into the element.

form_textfield = driver.find_element_by_name('username')
form_textfield.send_keys("admin")

search.send_keys("海贼王", Keys.ARROW_DOWN) # 回车

This can also be used to set file inputs.

file_input = driver.find_element_by_name('profilePic')
file_input.send_keys("path/to/profilepic.gif")

示例:selenium登录知乎

selenium设置代理

from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()

# 设置代理
# 一定要注意,=两边不能有空格,不能是这样--proxy-server = http://202.20.16.82:10152
chromeOptions.add_argument("--proxy-server=http://10.3.132.6:808")
browser = webdriver.Chrome(chrome_options=chromeOptions)

# 查看本机ip,查看代理是否起作用
browser.get("https://blog.csdn.net/zwq912318834/article/details/78626739")
print(browser.page_source)

# 退出,清除浏览器缓存
# browser.quit()

练习:selenium登录QQ空间

提示:
    qq空间: https://i.qq.com/
        
    login = driver.find_element_by_id('login_frame')
    # iframe需要转换
    browser.switch_to_frame(login)

PhantomJS 无界面浏览器

已停止研发

headless

Headless Chrome是Chrome 浏览器的无界面形态,可以在不打开浏览器的前提下,使用所有 Chrome 支持的特性运行程序。相比于现代浏览器,Headless Chrome 更加方便测试web应用,获得网站的截图,做爬虫抓取信息等,也更加贴近浏览器环境。

Headless Chrome基于PhantomJS(QtWebKit内核)由谷歌Chrome团队开发。团队表示将专注研发这个项目

确保你的 chrome 浏览器版本是 60+.

配置

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")  # 使用headless 无界面形态
chrome_options.add_argument('--disable-gpu')  # 禁用gpu

driver = webdriver.Chrome(chrome_options=chrome_options)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容