微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

day03元素交互操作

关于点击,清除

from selenium import webdriver #web驱动
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
from selenium.webdriver import  ActionChains
import time

driver = webdriver.Chrome(r'D:\Python\Scripts\chromedriver.exe')

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(5)

finally:
      driver.close()

关于ActionChans

#方法一(瞬移)

from selenium import webdriver #web驱动
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
from selenium.webdriver import  ActionChains
import time

driver = webdriver.Chrome(r'D:\Python\Scripts\chromedriver.exe')

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('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')
#启示方块瞬间移动到目标方块中
#拟定好一个动作,需要调用执行方法
     action.drag_and_drop(source,target).perform()
     time.sleep(10)
finally:
     driver.close()

#方法二(慢移)

from selenium import webdriver #web驱动
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
from selenium.webdriver import  ActionChains
import time

driver = webdriver.Chrome(r'D:\Python\Scripts\chromedriver.exe')

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('iframeResult')
     time.sleep(1)

    #启示方块id:drappable
     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轴
     # print(target.location)
     distance=target.location['x']-source.location['x']
#摁住起始滑块
     ActionChains(driver).click_and_hold(source).perform()

#循环移动

     s=0
     while s<distance:
         #获取动作链对象
         #每一次位移2位移
         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 #web驱动
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
import time

driver = webdriver.Chrome(r'D:\Python\Scripts\chromedriver.exe')

try:
     driver.implicitly_wait(10)
     driver.get('http://www.baidu.com/')

     driver.execute_script(
         '''
        alert("长歌依梦")
        '''
     )

     time.sleep(10)
finally:
     driver.close()

 

原文地址:https://www.cnblogs.com/changgeyimeng/p/11128249.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐