selenium 操作

定位
# 1By ID
# 2By Class Name
# 3By Tag Name
# 4By Name
# 5By Link Text
#   cheese = driver.find_element_by_link_text("cheese")
#   cheese = driver.find_element(By.LINK_TEXT, "cheese")
#   链接元素上面有文字描述的都可以采取find_element_by_link_text()方法来进行元素定位
#6By CSS
#   cheese = driver.find_element_by_css_selector("#food span.dairy.aged")
#         表示id为food的标签下的class为dairy aged的span标签
# 7By XPath



# 鼠标:
# #导入 ActionChains 类
# from selenium.webdriver import ActionChains
#
# # 鼠标移动到 ac 位置
# ac = driver.find_element_by_xpath('element')
# ActionChains(driver).move_to_element(ac).perform()
#
#
# # 在 ac 位置单击
# ac = driver.find_element_by_xpath("elementA")
# ActionChains(driver).move_to_element(ac).click(ac).perform()
#
# # 在 ac 位置双击
# ac = driver.find_element_by_xpath("elementB")
# ActionChains(driver).move_to_element(ac).double_click(ac).perform()
#
# # 在 ac 位置右击
# ac = driver.find_element_by_xpath("elementC")
# ActionChains(driver).move_to_element(ac).context_click(ac).perform()
#
# # 在 ac 位置左键单击hold住
# ac = driver.find_element_by_xpath('elementF')
# ActionChains(driver).move_to_element(ac).click_and_hold(ac).perform()
#
# # 将 ac1 拖拽到 ac2 位置
# ac1 = driver.find_element_by_xpath('elementD')
# ac2 = driver.find_element_by_xpath('elementE')
# ActionChains(driver).drag_and_drop(ac1, ac2).perform()



# 导入 Select 类
#from selenium.webdriver.support.ui import Select

# 找到 name 的选项卡
#select = Select(driver.find_element_by_name('status'))

#
# select.select_by_index(1)----索引从0开始
# select.select_by_value("0")-----value属性
# select.select_by_visible_text(u"未审核")------文本内容
# select.deselect_all()----全部取消选择

#获取弹窗及处理
#from selenium.webdriver.common.alert import Alert-----针对alter的操作
#alert = driver.switch_to_alert()----切换到alert
#a1 = Alert(driver)-------直接实例化Alert对象
        #以上两个的作用类似

# Alert(driver).accept()  # 等同于点击“确认”或“OK”
# Alert(driver).dismiss()  # 等同于点击“取消”或“Cancel”
# Alert(driver).authenticate(username,password)  # 验证,针对需要身份验证的alert,目前还没有找到特别合适的示例页面
# Alert(driver).send_keys(keysToSend)  # 发送文本,对有提交需求的prompt框(上图3)
# Alert(driver).text  # 获取alert文本内容,对有信息显示的alert框

#页面切换
#driver.switch_to.window("this is window name")---切换到指定标题的页面(并不是页面title,不可以通过title以及url来切换页面)
# driver.current_window_handle----获得当前页面的句柄
# driver.window_handles------获得所有页面的句柄
#*****关闭了新窗口之后,driver并不会自动跳转回原窗口,而是需要你switch回来,直接去定位窗口元素会报NoSuchElementException

# 页面前进或后退
# driver.forward()     #前进
# driver.back()        # 后退

#div类对话框
#div类对话框是普通的网页元素,通过正常的find_element就可以定位并进行操作。不在这里进行详述。注意设置一定的等待时间,
# 以免还未加载出来便进行下一步操作,造成NoSuchElementException报错。


#删除cookies
# By name
#driver.delete_cookie("CookieName")
# all
#driver.delete_all_cookies()

#页面等待
# from selenium.webdriver.support.ui import WebDriverWait----等待库
# from selenium.webdriver.support import expected_conditions as EC-----负责条件出发(场景判断方法)
#     显式等待
#             try:
#                 # 页面一直循环,直到 id="myDynamicElement" 出现----如果超出设置的最大等待时间,就会抛出异常,间隔0.5秒扫描一次。
#                 element = WebDriverWait(driver, 10).until(
#                     EC.presence_of_element_located((By.ID, "myDynamicElement"))
#                 )
#             finally:
#                 driver.quit()
#     详解连接:http://www.mamicode.com/info-detail-1720150.html
#     隐式等待
#             driver.implicitly_wait(xx)---隐性等待,最长等30秒

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值