day03 元素交互操作
'''''' ''' 点击、清除 ''' # from selenium import webdriver # from selenium.webdriver import ActionChains # from selenium.webdriver.common.keys import Keys # 键盘按键操作 # import time # # # # driver = webdriver.Chrome() # # try: # driver.implicitly_wait(10) # driver.get('https://www.jd.com/') # time.sleep(5) # # # 点击、清除 # input = driver.find_element_by_id('key') # input.send_keys('围城') # # # 通过class查找搜索按钮 # search = driver.find_element_by_class_name('button') # search.click() # 点击搜索按钮 # # time.sleep(3) # # input2 = driver.find_element_by_id('key') # input2.clear() # 清空输入框 # # time.sleep(1) # # input2.send_keys('墨菲定律') # input2.send_keys(Keys.ENTER) # # time.sleep(10) # # finally: # driver.close() ''' ActionChains: 动作链 ''' # from selenium import webdriver # from selenium.webdriver import ActionChains # from selenium.webdriver.common.keys import Keys # 键盘按键操作 # import time # # # # driver = webdriver.Chrome() # try: # driver.implicitly_wait(10) # driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable') # time.sleep(5) # # # 遗弃方法 # # driver.switch_to_frame() # # 新方法 # driver.switch_to.frame('iframeResult') # time.sleep(1) # # # 获取动作链对象 # action = ActionChains(driver) # # # 起始方块id: draggable # source = driver.find_element_by_id('draggable') # # # 目标方块id: droppable # target = driver.find_element_by_id('droppable') # # # 方式一: 秒移 # # 起始方块瞬间移动到目标方块中 # # 拟定好一个动作,需要调用执行方法perform # # action.drag_and_drop(source, target).perform() # # # time.sleep(10) # # finally: # driver.close() # from selenium import webdriver # from selenium.webdriver import ActionChains # from selenium.webdriver.common.keys import Keys # 键盘按键操作 # import time # # driver = webdriver.Chrome() # try: # driver.implicitly_wait(10) # driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable') # time.sleep(5) # # # 遗弃方法 # # driver.switch_to_frame() # # 新方法 # driver.switch_to.frame('iframeResult') # time.sleep(1) # # # 起始方块id: draggable # source = driver.find_element_by_id('draggable') # # # 目标方块id: droppable # target = driver.find_element_by_id('droppable') # # print(source.size) # 大小 # print(source.tag_name) # 标签名 # print(source.text) # 文本 # print(source.location) # 坐标: X与Y轴 # # # 找到滑动距离 # distance = target.location['x'] - source.location['x'] # # # 摁住起始滑块 # ActionChains(driver).click_and_hold(source).perform() # # # 方式二: 一点一点移动 # s = 0 # while s < distance: # # 获取动作链对象 # # 每一次位移s距离 # ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform() # s += 2 # # time.sleep(0.1) # # # 松开起始滑块 # ActionChains(driver).release().perform() # # # time.sleep(10) # # finally: # driver.close() ''' 执行js代码 ''' from selenium import webdriver import time driver = webdriver.Chrome() try: driver.implicitly_wait(10) driver.get('https://www.baidu.com/') driver.execute_script( ''' alert("浙江万里学院,是浙江最牛皮的学院!") ''' ) time.sleep(10) finally: driver.close()