Selenium 安装和测试

来源:互联网 发布:moe破解软件下载 编辑:程序博客网 时间:2024/06/04 19:09

>>>pip install -Uselenium

 

  1. 对于Chrome

1下载Chrome drive(是个独立的exe)

https://sites.google.com/a/chromium.org/chromedriver/home

 

参考文档做设置:

https://sites.google.com/a/chromium.org/chromedriver/getting-started

 

测试代码:

 

fromselenium import webdriver

fromselenium.common.exceptions import NoSuchElementException

fromselenium.webdriver.common.keys import Keys

importtime

 

browser =webdriver.Chrome('C:/Program Files (x86)/chromedriver.exe')  # Optional argument, if not specified willsearch path.

browser.get("http://www.yahoo.com")# Load page

assert"Yahoo!" in browser.title

elem =browser.find_element_by_name("p1") # Find the query box

elem.send_keys("seleniumhq"+ Keys.RETURN)

time.sleep(0.2)# Let the page load, will be added to the API

try:

   browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")

exceptNoSuchElementException:

    assert 0, "can't find seleniumhq"

browser.close()

原创粉丝点击