前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python selenium定位元素

python selenium定位元素

作者头像
用户5760343
发布2022-05-13 10:33:21
1.6K0
发布2022-05-13 10:33:21
举报
文章被收录于专栏:sktj

查找元素: find_element_by_id find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_partial_link_text find_element_by_tag_name find_element_by_class_name find_element_by_css_selector 查找多个元素: find_elements_by_name find_elements_by_xpath find_elements_by_link_text find_elements_by_partial_link_text find_elements_by_tag_name find_elements_by_class_name find_elements_by_css_selector

xpath定位元素

username = driver.find_element_by_xpath("//form[input/@name='username']") username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]") username = driver.find_element_by_xpath("//input[@name='username']")

填写输入框

elem_user = driver.find_element_by_name("username") elem_user.clear elem_user.send_keys("15201615157")

发送确认键

elem_pwd = driver.find_element_by_name("password") elem_pwd.clear elem_pwd.send_keys("******") elem_pwd.send_keys(Keys.RETURN)

退出

driver.close() driver.quit()

获取元素的属性:

size 尺寸 text get_attribute() location page_source driver.title 标题 current_url 当前页面URL is_display() is_enabled() is_selected() 判断元素是否被选中 tag_name 返回元素的tagName

example

from selenium import webdriver from selenium.webdriver.common.keys import Keys import time

driver = webdriver.PhantomJS(executable_path="G:\phantomjs-1.9.1-windows\phantomjs.exe") driver.get("http://www.baidu.com/")

size = driver.find_element_by_name("wd").size print size

尺寸: {'width': 500, 'height': 22}

news = driver.find_element_by_xpath("//div[@id='u1']/a[1]").text print news

文本: 新闻

href = driver.find_element_by_xpath("//div[@id='u1']/a[2]").get_attribute('href') name = driver.find_element_by_xpath("//div[@id='u1']/a[2]").get_attribute('name') print href,name

属性值: http://www.hao123.com/ tj_trhao123

location = driver.find_element_by_xpath("//div[@id='u1']/a[3]").location print location

坐标: {'y': 19, 'x': 498}

print driver.current_url

当前链接: https://www.baidu.com/

print driver.title

标题: 百度一下, 你就知道

result = location = driver.find_element_by_id("su").is_displayed() print result

是否可见: True

ActionChains模拟鼠标操作

context_click(elem) 右击鼠标点击元素elem,另存为等行为 double_click(elem) 双击鼠标点击元素elem,地图web可实现放大功能 drag_and_drop(source,target) 拖动鼠标,源元素按下左键移动至目标元素释放 move_to_element(elem) 鼠标移动到一个元素上 click_and_hold(elem) 按下鼠标左键在一个元素上 perform() 在通过调用该函数执行ActionChains中存储行为

将图片另存为

import time from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox() driver.get("http://www.baidu.com")

鼠标移动至图片上 右键保存图片

elem_pic = driver.find_element_by_xpath("//div[@id='lg']/img") print elem_pic.get_attribute("src") action = ActionChains(driver).move_to_element(elem_pic) action.context_click(elem_pic)

重点:当右键鼠标点击键盘光标向下则移动至右键菜单第一个选项

action.send_keys(Keys.ARROW_DOWN) time.sleep(3) action.send_keys('v') #另存为 action.perform()

获取另存为对话框(失败)

alert.switch_to_alert() alert.accept()

Actions action = new Actions(driver);action.click();// 鼠标左键在当前停留的位置做单击操作 action.click(driver.findElement(By.name(element)))// 鼠标左键点击指定的元素

鼠标右击

Actions action = new Actions(driver); action.contextClick();// 鼠标右键在当前停留的位置做单击操作 action.contextClick(driver.findElement(By.name(element)))// 鼠标右键点击指定的元素

鼠标双击

Actions action = new Actions(driver); action.doubleClick();// 鼠标在当前停留的位置做双击操作 action.doubleClick(driver.findElement(By.name(element)))// 鼠标双击指定的元素

鼠标拖拽

Actions action = new Actions(driver); // 鼠标拖拽动作,将 source 元素拖放到 target 元素的位置。 action.dragAndDrop(source,target); // 鼠标拖拽动作,将 source 元素拖放到 (xOffset, yOffset) 位置,其中 xOffset 为横坐标,yOffset 为纵坐标。 action.dragAndDrop(source,xOffset,yOffset);

鼠标悬停

Actions action = new Actions(driver); action.clickAndHold();// 鼠标悬停在当前位置,既点击并且不释放 action.clickAndHold(onElement);// 鼠标悬停在 onElement 元素的位置

鼠标移动

Actions action = new Actions(driver); action.moveToElement(toElement);// 将鼠标移到 toElement 元素中点 // 将鼠标移到元素 toElement 的 (xOffset, yOffset) 位置, //这里的 (xOffset, yOffset) 是以元素 toElement 的左上角为 (0,0) 开始的 (x, y) 坐标轴。 action.moveToElement(toElement,xOffset,yOffset) // 以鼠标当前位置或者 (0,0) 为中心开始移动到 (xOffset, yOffset) 坐标轴 action.moveByOffset(xOffset,yOffset);

鼠标释放

Actions action = new Actions(driver); action.release();// 释放鼠标

模拟键盘

Actions action = new Actions(driver); action.sendKeys(Keys.TAB);// 模拟按下并释放 TAB 键 action.sendKeys(Keys.SPACE);// 模拟按下并释放空格键 /*** 针对某个元素发出某个键盘的按键操作,或者是输入操作, 比如在 input 框中输入某个字符也可以使用这个方法。这个方法也可以拆分成: action.click(element).sendKeys(keysToSend)。 */ action.sendKeys(element,keysToSend);

组合键

Actions action = new Actions(driver); action.keyDown(Keys.CONTROL);// 按下 Ctrl 键 action.keyDown(Keys.SHIFT);// 按下 Shift 键 action.keyDown(Key.ALT);// 按下 Alt 键 action.keyUp(Keys.CONTROL);// 释放 Ctrl 键 action.keyUp(Keys.SHIFT);// 释放 Shift 键 action.keyUp(Keys.ALT);// 释放 Alt 键

ALT+F4

所以要通过 Alt+F4 来关闭当前的活动窗口,可以通过下面语句来实现:action.keyDown(Keys.ALT).keyDown(Keys.F4).keyUp(Keys.ALT).perform();

组合键

而如果是对于像键盘上面的字母键 a,b,c,d... 等的组合使用,可以通过以下语句实现 :action.keyDown(Keys.CONTROL).sednKeys(“a”).perform(); ################################################## 上传文件脚本 /**

  • @Description: 在百度云上测试文件批量上传功能,主要是通过循环的方式去做单一
  • 的上传动作 , 登陆过程已经去掉 */ @Test public void test_mutilUploadFile() throws Exception { System.out.println("upload start"); // 获取上传控件元素 WebElement uploadButton = driver.findElement(By.name("html5uploader")); // 构建上传文件路径,将需要上传的文件添加到 CharSequence 数组 CharSequence[] files = new CharSequence[5]; files[0] = "C:\test\test1.txt"; files[1] = "C:\test\test2.txt"; files[2] = "C:\test\test3.txt"; files[3] = "C:\test\test4.txt"; files[4] = "C:\test\test5.txt"; // 循环列出每支需要上传的文件路径,做单一上传动作 for(CharSequence file: files){ uploadButton.sendKeys(file); } Thread.sleep(2000); System.out.println("upload end"); }

send_keys(Keys.ENTER) 按下回车键 send_keys(Keys.TAB) 按下Tab制表键 send_keys(Keys.SPACE) 按下空格键space send_keys(Kyes.ESCAPE) 按下回退键Esc send_keys(Keys.BACK_SPACE) 按下删除键BackSpace send_keys(Keys.SHIFT) 按下shift键 send_keys(Keys.CONTROL) 按下Ctrl键 send_keys(Keys.ARROW_DOWN) 按下鼠标光标向下按键 send_keys(Keys.CONTROL,'a') 组合键全选Ctrl+A send_keys(Keys.CONTROL,'c') 组合键复制Ctrl+C send_keys(Keys.CONTROL,'x') 组合键剪切Ctrl+X send_keys(Keys.CONTROL,'v') 组合键粘贴Ctrl+V

屏幕截图 driver.save_screenshot('baidu.png')

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • xpath定位元素
  • 填写输入框
    • 发送确认键
      • 退出
        • 获取元素的属性:
          • 将图片另存为
      • example
      • 尺寸: {'width': 500, 'height': 22}
      • 文本: 新闻
      • 属性值: http://www.hao123.com/ tj_trhao123
      • 坐标: {'y': 19, 'x': 498}
      • 当前链接: https://www.baidu.com/
      • 标题: 百度一下, 你就知道
      • 是否可见: True
      • ActionChains模拟鼠标操作
      • 鼠标移动至图片上 右键保存图片
      • 重点:当右键鼠标点击键盘光标向下则移动至右键菜单第一个选项
      • 获取另存为对话框(失败)
        • 鼠标右击
        • 鼠标双击
          • 鼠标拖拽
            • 鼠标悬停
              • 鼠标移动
                • 鼠标释放
                  • 组合键
                  • ALT+F4
              • 模拟键盘
                • 组合键
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档