selenium webdriver常用方法
selenium webdriver常用方法
1. selenium webdriver常用方法
参考文档:https://www.seleniumhq.org/docs/03_webdriver.jsp
1.1. 查找元素 locate ui elements
参考文档:https://www.seleniumhq.org/docs/03_webdriver.jsp
查找方法从引用方法上可分为两种:直接引用,参数引用
示例:
element = driver.find_element_by_id("coolestWidgetEvah")
or
from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")
常用定位方法:
element = driver.find_element_by_id("coolestWidgetEvah")
cheeses = driver.find_elements_by_class_name("cheese")
frame = driver.find_element_by_tag_name("iframe")
cheese = driver.find_element_by_name("cheese")
cheese = driver.find_element_by_link_text("cheese")
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")
inputs = driver.find_elements_by_xpath("//input")
#使用javascript
element = driver.execute_script("return $('.cheese')[0]")
当然,查找多个元素也有相应方法,在方法名element后加个s,示例如下:
input_first = browser.find_elements_by_id('q')
1.2. 获取text值:
element = driver.find_element_by_id("element_id")
1.3. 表单处理User Input - Filling In Forms
找到表单元素:
select = driver.find_element_by_tag_name("select")
allOptions = select.find_elements_by_tag_name("option")
for option in allOptions:
print "Value is: " + option.get_attribute("value")
option.click()
上例找到了一个表单,并遍历打印值。
# available since 2.12
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_tag_name("select"))
select.deselect_all()
select.select_by_visible_text("Edam")
取消选择所有的option,然后根据text选择一个option。
提交表单:
一种方法是提交按钮:
driver.find_element_by_id("submit").click()
另一种方法:
webdriver在每个元素上都有submit方法,如果在表单中调用,会向上递归直到找到一个封闭的表单然后submit;If the element isn’t in a form, then the NoSuchElementException will be thrown:
element.submit()
1.4. cookies
增删查
# Go to the correct domain
driver.get("http://www.example.com")
# Now set the cookie. Here's one for the entire domain
# the cookie name here is 'key' and its value is 'value'
driver.add_cookie({'name':'key', 'value':'value', 'path':'/'})
# additional keys that can be passed in are:
# 'domain' -> String,
# 'secure' -> Boolean,
# 'expiry' -> Milliseconds since the Epoch it should expire.
# And now output all the available cookies for the current URL
for cookie in driver.get_cookies():
print "%s -> %s" % (cookie['name'], cookie['value'])
# You can delete cookies in 2 ways
# By name
driver.delete_cookie("CookieName")
# Or all of them
driver.delete_all_cookies()
1.5. useragent
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "some UA string")
driver = webdriver.Firefox(profile)
1.6. Moving Between Windows and Frames
Some web applications have many frames or multiple windows. WebDriver supports moving between named windows using the “switchTo” method:
driver.switch_to.window("windowName")
driver.switch_to.frame("frameName")
1.7. navigation:history and location
简单来说,就是前一页和后一页。
driver.get("http://www.example.com") # python doesn't have driver.navigate可以忽略,使用get即可
driver.forward()
driver.back()
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号