Article Directory
0 Environment Configuration
System: Win7
Python Version: 3.6.4
the Selenium Version: 3.14.0
Firefox: 65.0.2 (64)
geckodriver: geckodriver-v0.21.0-win64.zip
Handle switch 1
Sometimes when we use selenium, simulated in a click, a new window or a new tab will appear. However, at this time, our driver did not update the login window when analog or start page (in fact, is a handle, "handle is a special smart pointer", the handle to the beginning of the page). However, when I click action occurs, switch pages happen, but still handle original handle, then we need to switch operating handles to adjust to the new interface appears. Specific operation is as follows:
next_click.click() # 模拟点击下一页的时候,会出现一个新窗口或者新标签
n = drive.window_handles # 这个时候会生成一个新窗口或新标签页的句柄,代表这个窗口的模拟driver
print('当前句柄: ', n) # 会打印所有的句柄
drive.switch_to_window(n[-1]) # driver切换至最新生产的页面
2 Other selenium reptiles skills
2.1 to access the contents of a tab under properties
For example, I want to get this python clicks at url:
url = drive.find_element_by_xpath(').get_attribute('href') # 获取某一页中的,某个标签下某个属性的内容
2.2 turn off the current window, tab
The current drive handle is that you need to close the window, tab page:
drive.close() # 注: 当前drive的句柄是你需要关闭窗口、标签页的
2.3 simulated mouse verification sliding bar
Sometimes, a simple question will meet verification slider, like this:
At this point, need to simulate sliding a mouse, refer to the following:
slider = drive.find_element_by_xpath('***') # 滑动条那个小块的位置
velocity = [5, 6, 19, 40, 40, 50] # 150长度, 根据进度条的长度,来设置滑动距离
ActionChains(drive).click_and_hold(slider).perform() # 点击滑块,并保持
for v in velocity:
c = random.randint(0, 2) # 为了反反爬虫,设计不定操作
v = v+c # 加入不定因素
ActionChains(drive).move_by_offset(xoffset=v, yoffset=0).perform() # 按照预先设计速率滑动
ActionChains(drive).release().perform() # 释放