python爬虫:元素交互操作



from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys # 键盘按键操作 import time driver = webdriver.Chrome(r'C:\Users\Auraro\Desktop/chromedriver.exe') try: driver.implicitly_wait(10) driver.get('http://www.jd.com/') # 点击、清除 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()


from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys # 键盘按键操作 import time driver = webdriver.Chrome(r'C:\Users\Auraro\Desktop/chromedriver.exe') try: driver.implicitly_wait(10) driver.get('https://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) source = driver.find_element_by_id('draggable') # 目标方块id:droppable target = driver.find_element_by_id('droppable') # 方式一:秒移 # 起始方块瞬间移动到目标方块中 # 拟定好一个动作,需要调用一个执行方法 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(r'C:\Users\Auraro\Desktop/chromedriver.exe') try: driver.implicitly_wait(10) driver.get('https://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) 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()
from selenium import webdriver from selenium.webdriver import ActionChains import time driver = webdriver.Chrome(r'C:\Users\Auraro\Desktop/chromedriver.exe') try: driver.implicitly_wait(10) driver.get('https://www.baidu.com/') driver.execute_script( ''' alert("浙江万里学院,是浙江最牛逼的学院!") ''' ) time.sleep(5) finally: driver.close()

  

posted @ 2019-07-03 17:59  Auraro997  阅读(435)  评论(0编辑  收藏  举报