# coding=utf-8 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import Select from selenium.webdriver.common.action_chains import ActionChains import time profile_path = r'C:\Users\songping\AppData\Roaming\Mozilla\Firefox\Profiles\gjihjypg.default' profile = webdriver.FirefoxProfile(profile_path) driver = webdriver.Firefox(profile) url = "https://www.baidu.com/" driver.get(url) mouse = driver.find_element_by_id("s-usersetting-top") ActionChains(driver).move_to_element(mouse).perform() s = driver.find_element_by_link_text("高级搜索") print(s.is_displayed())
# coding=utf-8 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import Select from selenium.webdriver.common.action_chains import ActionChains import time profile_path = r'C:\Users\songping\AppData\Roaming\Mozilla\Firefox\Profiles\gjihjypg.default' profile = webdriver.FirefoxProfile(profile_path) driver = webdriver.Firefox(profile) url = "https://www.baidu.com/" driver.get(url) mouse = driver.find_element_by_id("s-usersetting-top") ActionChains(driver).move_to_element(mouse).perform() s2 = driver.find_element_by_id("q5_0") print(s2.is_selected())
# coding=utf-8 from selenium import webdriver import time profile_path = r'C:\Users\songping\AppData\Roaming\Mozilla\Firefox\Profiles\gjihjypg.default' profile = webdriver.FirefoxProfile(profile_path) driver = webdriver.Firefox(profile) url = "https://www.baidu.com/" driver.get(url) time.sleep(3) def is_element_exist(locator): '''判断元素是否存在,结果返回布尔值 ture 或 false''' try: driver.find_element(*locator) print("找到元素了!!!!!") return True except: print("找不到元素") return False loc = ("id","su") t = is_element_exist(loc) print(t)
Expected_conditions模块提供了判断页面元素的16种方法:
1.title_is:判断当前页面的title是否完全等于(==)预期字符串,返回布尔值
2.title_contains : 判断当前页面的title是否包含预期字符串,返回布尔值3.presence_of_element_located : 判断某个元素是否被加到了dom树里,并不代表该元素一定可见
4.visibility_of_element_located : 判断某个元素是否可见. 可见代表元素非隐藏,并且元素的宽和高都不等于
5.0visibility_of : 跟上面的方法做一样的事情,只是上面的方法要传入locator,这个方法直接传定位到的element就好了
6.presence_of_all_elements_located : 判断是否至少有1个元素存在于dom树中。举个例子,如果页面上有n个元素的class都是'column-md-3',那么只要有1个元素存在,这个方法就返回
7.Truetext_to_be_present_in_element : 判断某个元素中的text是否 包含了预期的字符串
8.text_to_be_present_in_element_value : 判断某个元素中的value属性是否包含 了预期的字符串
9.frame_to_be_available_and_switch_to_it : 判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回
10.Falseinvisibility_of_element_located : 判断某个元素中是否不存在于dom树或不可见
11.element_to_be_clickable : 判断某个元素中是否可见并且是enable的,这样的话才叫clickable
12.staleness_of : 等某个元素从dom树中移除,注意,这个方法也是返回True或False
13.element_to_be_selected : 判断某个元素是否被选中了,一般用在select下拉列表
14.element_selection_state_to_be : 判断某个元素的选中状态是否符合预期
15.element_located_selection_state_to_be : 跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locator
16.alert_is_present : 判断页面上是否存在alert
标签:profile,webdriver,13,判断,元素,driver,element,import From: https://www.cnblogs.com/pingzi66-ww/p/17100291.html