''' 三种等待方法: 1.强制等待sleep(xx) 强制等待,不管你浏览器是否加载完了,程序都得等待,时间一到,继续执行下面的代码,作为调试很有用,有时候也可以在代码里这样等待,不过不建议总用这种等待方式,太死板,严重影响程序执行速度。 2.隐性等待implicitly_wait(xx) 隐形等待是设置了一个最长等待时间,如果在规定时间内网页加载完成,则执行下一步,否则一直等到时间截止,然后执行下一步。有些时候我们不需要等到所有元素加载完成,我想等我要的元素出来之后就立马执行下一步,那就应该用显性等待。 3.显性等待WebDriverWait(driver,timeout,xx),配合该类的until()和until_not()方法 程序每隔xx秒看一眼,如果条件成立了,则执行下一步,否则继续等待,直到超过设置的最长时间,然后抛出TimeoutException。 判断条件:expected_conditions 导入模块:selenium.webdriver.support.expected_conditions ''' #coding=utf-8 from selenium import webdriver from time import sleep from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By #设置浏览器 driver = webdriver.Firefox() url = "https://www.baidu.com" timeout = 20 driver.implicitly_wait(10) #隐式等待和显示等待都存在时,超时时间取二者中较大的 driver.get(url) #强制等待:强制等3秒再执行下一步 #sleep(3) #隐性等待:隐形等待是设置了一个最长等待时间,如果在规定时间内网页加载完成,则执行下一步,否则一直等到时间截止,然后执行下一步 #隐性等待对整个driver的周期都起作用,所以只要设置一次即可 #driver.implicitly_wait(30) #显性等待:WebDriverWait,配合该类的until()和until_not()方法,就能够根据判断条件而进行灵活地等待了。 #它主要的意思就是:程序每隔xx秒看一眼,如果条件成立了,则执行下一步,否则继续等待,直到超过设置的最长时间,然后抛出TimeoutException #until方法:当某元素出现或什么条件成立则继续执行 #until_not方法:当某元素消失或什么条件不成立则继续执行 #expected_conditions:selenium的一个模块,其中包含一系列可用于判断的条件: #以下两个条件类验证title,验证传入的参数title是否等于或包含于driver.title #title_is #title_contains #以下两个条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID, ‘kw’) #顾名思义,一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行 #presence_of_element_located #presence_of_all_elements_located #以下两个条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value #text_to_be_present_in_element #text_to_be_present_in_element_value #以下条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElement #frame_to_be_available_and_switch_to_it #以下条件判断是否有alert出现 #alert_is_present #以下条件判断元素是否可点击,传入locator #element_to_be_clickable #以下四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组 #第三个传入WebElement对象以及状态,相等返回True,否则返回False #第四个传入locator以及状态,相等返回True,否则返回False #element_to_be_selected #element_located_to_be_selected #element_selection_state_to_be #element_located_selection_state_to_be #最后一个条件判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新了 #staleness_of locator = (By.LINK_TEXT,"设置") try: print(WebDriverWait(driver,timeout).until(EC.title_is(u"百度一下,你就知道"))) print(WebDriverWait(driver,timeout).until(EC.title_contains(u"知道"))) WebDriverWait(driver,timeout).until(EC.presence_of_element_located(locator)) WebDriverWait(driver,timeout).until(EC.visibility_of_element_located((By.ID,'su'))) WebDriverWait(driver,timeout).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id='u1']/a[8]"),u"设置")) WebDriverWait(driver,timeout).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),u'百度一下')) WebDriverWait(driver,timeout).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap'))) WebDriverWait(driver,timeout).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='u1']/a[8]"))).click() #WebDriverWait(driver,timeout).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"))) ''' instance = WebDriverWait(driver,timeout).until(EC.alert_is_present()) print(instance.text()) instance.accept() ''' '''判断某个页面上是否存在alert,如果有alert则返回alert内容,并通过''' print(driver.find_element_by_link_text("设置").get_attribute('href')) # 打印当前页面title print(driver.title) # 打印当前页面URL print(driver.current_url) finally: #关闭当前页面 driver.close() #退出浏览器 #driver.quit()