PYTHON-SELENUM3 the third day basic api--browser operation

Source: Internet
Author: User

Open the Web page and verify

#验证网页#打开火狐driver = webdriver.Firefox(executable_path = "d:\\geckodriver")#将地址赋值给变量fURLfURL = "https://www.baidu.com"#浏览driver.get(fURL)


Browser refreshes forward and backward

#打开火狐driver = webdriver.Firefox(executable_path = "d:\\geckodriver")#将地址赋值给变量fURL和sURLfURL = "http://www.baidu.com"sURL = "http://www.sogou.com"#get方法打开百度页面之后再打开搜狗driver.get(fURL)driver.get(sURL)#后退到百度driver.back()#assert进行验证 没有发现这几个字,出现后面的提示assert driver.title.find(u"百度一下") >= 0,"没有出现"#前进到搜狗driver.forward()#assert进行验证 没有发现这几个字,出现后面的提示assert driver.title.find(u"搜狗") >=0,"没有出现"#刷新当前页面driver.refresh()

Browser maximization

#打开浏览器driver = webdriver.Firefox(executable_path = "d:\\geckodriver")#将网址赋值给fURL变量fURL = "http://www.baidu.com"#登录百度driver.get(fURL)#将窗口最大化driver.maximize_window()


Get the Title property value and determine what is usually done to prove that the Web page is open correctly

#打开浏览器driver = webdriver.Firefox(executable_path = "d:\\geckodriver")#将网址赋值给fURL变量fURL = "http://www.baidu.com"#进入百度首页driver.get(fURL)#窗口最大化driver.maximize_window()#获取title属性并赋值给变量titletitle = driver.title#控制台输出title信息print("当前网页的title属性是:",title)#断言编写 下面的断言是unittest框架的断言方法 后期会用到 这里只是为了属性编写#assertEqual(title,u"百度一下,你就知道","网页title属性值错误")

Gets the source code for the HTML and asserts that

driver = webdriver.Firefox(executable_path = "d:\\geckodriver")fURL = "http://www.baidu.com"driver.get(fURL)driver.maximize_window()#获取网页HTML源代码 并赋值给pageSourcepageSource = driver.page_source#在控制台打印print(pageSource)#断言编写,查看源代码中是否有贴吧这两个字,一样是unittest框架断言#assertTrue(u"贴吧" in pageSource,"没有找到贴吧这两个字")

View the site address and assert

driver = webdriver.Firefox(executable_path="d:\\geckodriver")fURL = "http://www.baidu.com"driver.get(fURL)#获取地址并赋值给pageUrlpageUrl = driver.current_url#在控制台打印print(pageUrl)#断言编写,unittest框架断言#assertEqual(pageUrl,"http://www.baidu.com","当前网页网址非预期结果")

Window jump

driver = webdriver.Firefox(executable_path="d:\\geckodriver")fURL = "https://news.baidu.com/"driver.get(fURL)#隐式等待driver.implicitly_wait(10)#获取当前窗口句柄 并赋值给handlehandle = driver.current_window_handle#验证控制台打印print(handle)#定位网页、贴吧等链接整体赋值给ss = driver.find_element_by_partial_link_text("网页")#点击第一个按钮s.click()#显示所有的句柄赋值给all_hall_h = driver.window_handles#循环判断是否与首页句柄相等for i in all_h:    if i != handle:        #如果不等于就切换        driver.switch_to.window(i)print(driver.title)#验证if u"百度一下" in driver.title:    print(u"页面打开正常")else:    print(u"失败")#返回driver.switch_to.window(handle)

PYTHON-SELENUM3 the third day basic api--browser operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.