selenuim学习

见过最好的学习教程: http://www.python3.vip/tut/auto/selenium/01/

这里感谢“白月黑羽”老师!

对应教学视频:《Python + Selenium Web自动化 全套教程

基础语句学习记录:

from selenium import webdriver

# 创建 WebDriver 对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'C:Program Files (x86)GoogleChromeApplicationchromedriver.exe')


############################################ html内部跳转功能
wd.get('http://cdn1.python3.vip/files/selenium/sample2.html') # 先根据name属性值 'innerFrame',切换到iframe中 wd.switch_to.frame('innerFrame') # 根据 class name 选择元素,返回的是 一个列表 elements = wd.find_elements_by_class_name('plant')
# wd.find_element_by_id('1')
for element in elements: print(element.text) wd.switch_to.default_content() # 然后再 选择操作 外部的 HTML 中 的元素 wd.find_element_by_id('outerbutton').click() wd.quit() ############################################ 设定最长等待时长 wd.implicitly_wait(10)

wd.get(
'http://cdn1.python3.vip/files/selenium/sample3.html') # 点击打开新窗口的链接 link = wd.find_element_by_tag_name("a") link.click() # wd.title属性是当前窗口的标题栏 文本 print(wd.title)

element.get_attribute('value')

for handle in wd.window_handles: # 先切换到该窗口 wd.switch_to.window(handle) # 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口 print(wd.title) if 'Bing' in wd.title: # 如果是,那么这时候WebDriver对象就是对应的该该窗口,正好,跳出循环, break

# mainWindow变量保存当前窗口的句柄

mainWindow = wd.current_window_handle

wd.switch_to.window(mainWindow)

# 点击打开新窗口的链接

link = wd.find_element_by_tag_name("a")

link.click()

原文地址:https://www.cnblogs.com/andylhc/p/14316767.html