fox.find_element_by_id(‘su’).send_keys(‘清安无别事’)

ele = fox.find_element_by_id(‘kw’)

pad = WebDriverWait(fox,10).until_not(lambda x:ele.is_displayed())

print(pad)

ele.click()

fox.quit()


        补充:lambda四种常用用法



> 
> 1、将lambda函数赋值给一个变量,通过这个变量间接调用该lambda函数。  
>  2、将lambda函数赋值给其他函数,从而将其他函数用该lambda函数替换。  
>  3、将lambda函数作为其他函数的返回值,返回给调用者。  
>  4、将lambda函数作为参数传递给其他函数
> 
> 
> 


        上述除了display用法还有两种用法:



> 
> ele.is\_displayed():判断某个元素是否显示页面上 
> 
> 
> ele.is\_selected():判断某个元素是否被选中 
> 
> 
> ele.is\_enables():判断某个元素是否可以操作,如判断input、select等元素的可编辑状态,如按钮 是否可以点击
> 
> 
> 


### Expected\_conditions使用思路


        expected\_conditions是Selenium的一个模块,主要用于对页面元素的加载进行判断,包括元素是否 存在,可点击等等。


        Expected Conditions的使用场景一版有两种:


                1. 直接在断言中使用


                2. 与WebDriverWait配合使用,显示等待页面上元素出现或者消失。


        一般情况下,我们在使用expected\_conditions模块时都会对其进行重命名,通过as关键字对其重命名为EC,所以也叫EC模块。



> 
>         导包:from selenium.webdriver.support import expected\_conditions as EC
> 
> 
>         1. title\_is(title) 
> 
> 
>                 title\_is(title)判断网页title是否是特定文本(英文区分大小写),若完全相同则返回True,否则 返回False。
> 
> 
>         2. title\_contains(title)
> 
> 
>                 title\_contains(title)判断网页title是否包含特定文本(英文区分大小写),若包含则返回True, 不包含返回False。
> 
> 
> 



  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.

from selenium import webdriver

from selenium.webdriver.support import expected_conditions as EC


fox = webdriver.Firefox()

fox.get(‘https://baidu.com’)

fox.implicitly_wait(5)

ec1 = EC.title_is(‘百度一下,你就知道’)(fox)

ec2 = EC.title_contains(‘百度一下’)(fox)

print(ec1,ec2)

fox.quit()



> 
>         3.  presence\_of\_element\_located(locator)判断一个元素存在于页面DOM树中,存在则返回元素本身,不存在则 报错。 
> 
> 
>         参数locator:定位器是一个数据类型元组 
> 
> 
>                         ("元素定位方式" , "方式对应的值") 
> 
> 
>                         ("id" , "id属性值") 或者(By.ID, 'id属性值')
> 
> 
>         4. presence\_of\_all\_elements\_located(locator)判断定位的元素范围内,至少有一个元素存在于页面当中,存在则 以list形式返回元素本身,不存在则报错。
> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


fox = webdriver.Firefox()

fox.get(‘https://baidu.com’)

fox.implicitly_wait(5)

ele = (By.ID,‘kw’)

pad1 = WebDriverWait(fox,10).until(EC.presence_of_element_located(ele))

pad1.send_keys(‘清安无别事’)


ele2 = (By.ID,‘su’)

pad2 = WebDriverWait(fox,10).until(EC.visibility_of_element_located(ele2),message=‘元素不可见’)

pad2.click()

fox.quit()


        这里我加了一个点,就是visibility\_of\_element\_located(),判断一个元素是否可见,这个方法也是一个比较常用的方法之一。


        除了上述所讲的这些用法,下面我列出了几种用法,自行借鉴哦,用法是类似的,看完上面的例子需要学会举一反三。
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.