selenium学习(二)

frame或iframe非常特殊,在html语法中,内部会包含一个嵌入的另一份html文档。
通过switch_to.frame切换到iframe里面去寻找html代码
通过src去定位到不同的frame标签
通过wd.switch_to.default_content() #切换到iframe外部

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

wd=webdriver.Chrome()  #将chromedriver.exe的目录放在环境变量中,即可不用调用service,直接调用浏览器

wd.get('https://www.douban.com/')
#通过switch_to.frame切换到iframe里面去寻找html代码

wd.switch_to.frame(wd.find_element(By.CSS_SELECTOR,'[src="//accounts.douban.com/passport/login_popup?login_source=anony"]'))   #通过src去定位到不同的frame标签

#wd.switch_to.default_content()   #切换到iframe外部

element=wd.find_element(By.CSS_SELECTOR,'.hd')
print(element.get_attribute('outerHTML'))
#print(element.text)

wd.quit()

浏览器切换窗口
可以使用webdriver对象的switch_to属性的window方法
如:wd.switch_to.window(handle)
其中,webdriver对象有window_handles属性,这是一个列表对象,包含了当前浏览器窗口的所有的窗口句柄。
通过类似下面代码:

mainwindows=wd.current.window_handle  #保存原始的窗口句柄
for handle in wd.window_handles:
	wd.switch_to.window(handle)
	if  '窗口的title'  in wd.title:   #判断是不是我们要操作的那个窗口
		break
wd.switch_to.window(mainwindows)   #切回主窗口

还有一些移动鼠标需要用到Actionchains属性,还有check,radio,select选择框的选取。

猜你喜欢

转载自blog.csdn.net/qq_44267691/article/details/124326297