1. <html>
  2. <head>
  3. <title></title>
  4. </head>
  5. <body>
  6. <span class="bg s_ipt_wr quickdelete-wrap">
  7. <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
  8. <a href="javascript:;" id="quickdelete" title="清空" class="quickdelete" style="top: 0px; right: 0px; display: none;" name="quickdelete"></a>
  9. <span class="soutu-hover-tip" style="display: none;">按图片搜索</span></span>
  10. </body>
  11. </html>

_

通过ID定位

  1. document.getElementById("id")

通过name定位

获取的是list,具体定位需要加上索引值

  1. document.getElementsByNmae("name")

通过class定位

获取的是list,具体定位需要加上索引值

  1. document.getElementsByClassName("className")
  1. # 实例
  2. from selenium import webdriver
  3. class Test_javascript:
  4. def test_js(self):
  5. driver = webdriver.Chrome()
  6. driver.maximize_window()
  7. driver.get('https://www.baidu.com')
  8. time.sleep(3)
  9. js = "document.getElementsByClassName('pf')[0].textContent"
  10. test = driver.execute_script(js)
  11. # 通过document.getElementsByClassName('pf')[0].textContent获取文本


通过tag定位

获取的是list,具体定位需要加上索引值

  1. document.getElementsByTagName("tag")

通过css定位

获取的是list,具体定位需要加上索引值

  1. document.querySelecter("css Selector")
  2. document.querySelectorAll("css Selector")
  1. # 实例
  2. from selenium import webdriver
  3. class Test_javascript:
  4. def test_js(self):
  5. driver = webdriver.Chrome()
  6. driver.maximize_window()
  7. driver.get('https://www.baidu.com')
  8. time.sleep(3)
  9. js = "document.querySelector('#s-usersetting-top').textContent"
  10. test = driver.execute_script(js)
  11. # 通过document.querySelecter定位获取文本

常见执行js方式

javaScript中有个内置对象:arguments,arguments包含了函数调用的参数数组,[0]表示数组的第一个值

  1. 方式一:
  2. js = "document.getElementsByClassName('pf')[0].textContent"
  3. test = driver.execute_script(js)
  4. 方式二:
  5. button = driver.find_element_by_css_selector("[name='tj_settingicon']")
  6. driver.execute_script("arguments[0].click()", button)

常见js定位使用场景

浏览器窗口滚动

window.scrollTo(),用于设置浏览器滚动条的水平位置和垂直位置

  1. js = "window.scrollTo(100, 500);"
  2. driver.execute_script(js)

滚动到页面底部

  1. driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")

滚动到页面顶部

  1. driver.execute_script("window.scrollTo(0, 0)")

元素聚焦:scrollintoView()

scrollintoView(false):与当前窗口底部对齐
scrollintoView(true):与当前窗口顶部对齐,默认

  1. button = driver.find_element_by_css_selector("[name='tj_settingicon']")
  2. driver.execute_script("arguments[0].scrollintoView()", button)

输入文本框:value

  1. js = "document.getElementsByClassName('pf')[0].value("测试")"
  2. test = driver.execute_script(js)

获取文本内容:textContent

  1. js = "document.getElementsByClassName('pf')[0].textContent"
  2. test = driver.execute_script(js)

点击事件:click

  1. js = "document.getElementsByClassName('pf')[0].click()"
  2. test = driver.execute_script(js)

时间控件操作

通过使用ActionChains类可以实现上下滑动选择日期,也可以通过TouchActions类实现,具体见如下:
使用TouchActions类中的scrollfrom_element()方法滑动元素,参数如下:
_on_element:滑动的元素

xoffset:x坐标
yoffset:y坐标

  1. # 定位要滑动的年、月、日
  2. downs = driver.find_element_by_id("id")
  3. year= downs[0]
  4. month= downs[1]
  5. day = downs[2]
  6. action = webdriver.TouchActions(driver)
  7. action.scroll_from_element(year, 0, 5).perform()
  8. action.scroll_from_element(month, 0, 30).perform()
  9. action.scroll_from_element(day, 0, 30).perform()


视频播放

提供了JavaScript接口和多种方法和属性;currentSrc:返回当前音频、视频的URLload():控制视频的加载play():控制视频的播放pause():控制视频的暂停

  1. video = driver.find_element_by_id("html3-api")
  2. # 返回播放文件地址
  3. url = driver.execute_script("return arguments[0].currentSrc;", video)
  4. #播放视频
  5. driver.execute_script("arguments[0].play()", video)
  6. #播放时间
  7. sleep(15)
  8. # 暂停播放
  9. driver.execute_script("arguments[0].pause", video)


滑动解锁

滑动解锁是目前比较流行的解锁方式,其实当我们单击滑块滑动,改变的只是CSS样式
click_and_hold():单击并按下鼠标左键
move_by_offest():移动鼠标,第一个参数为x坐标,第二个参数为y坐标
reset_action():重置action

  1. dragger = driver.find_elements_by_class_name("slide-to-unlock-handle")[0]
  2. action = ActionChains(driver)
  3. action.click_and_hold(dragger).perform() #鼠标左键按下不放
  4. for index in range(200):
  5. try:
  6. action.move_by_offset(2, 0).perform() # 平行移动鼠标
  7. except UnexpectedAlertPresentException:
  8. """当解锁成功后会抛UnexpectedAlertPresentException异常,捕捉后跳出循环"""
  9. break
  10. action.reset_actions()
  11. time.sleep(0.1)

window.scrollBy()
window.scrollBy(0,500) 向下滚动500个像素
window.scrollBy(0,-500) 向上滚动500个像素
window.scrollBy(500,0) 向右滚动500个像素
window.scrollBy(-500,0) 向左滚动500个像素