公司做的网站费用如何做账,erp管理系统的作用,常州做集装箱的公司,漳州做网站建设的公司selenium三大等待#xff0c;sleep#xff08;强制#xff09;、implicitlyWait#xff08;隐式等待#xff09;、WebDriverWait#xff08;显式等待#xff09;#xff0c;主要记一下最后面的WebDriverWait。
WebDriverWait是三大等待中最常用也是最好用的一种等待方…selenium三大等待sleep强制、implicitlyWait隐式等待、WebDriverWait显式等待主要记一下最后面的WebDriverWait。
WebDriverWait是三大等待中最常用也是最好用的一种等待方式比起另外两种而言B格更高、更智能。写法为
#WebDriverWait(driver,等待总时长,查询间隔时间).until(EC.visibility_of_element_located((By.XPATH,Xpath公式)))
# 例如
cj //span[text()19/05/27]/parent::p/parent::div/preceding-sibling::div[classannounce-cont clearfix]//a[text()查看成绩]
WebDriverWait(driver,30,0.5).until(EC.visibility_of_element_located((By.XPATH,cj))) 在iframe中也可以使用WebDriverwait例如
WebDriverWait(driver,30,0.5).until(EC.frame_to_be_available_and_switch_to_it(login_frame_qq))
# 其中login_frame_qq是iframe的name属性
# EC.frame_to_be_available_and_switch_to_it这句话的意思是等待并切换至也就是说这句话等同EC.visibility_of_element_locateddriver.switch_to_frame
例子中的等待含义为总时长30S每间隔0.5S查询一次until括号中的函数。EC为selenium.webdriver.support.expected_conditions的缩写visibility_of_element_located的用法是查询后跟元祖中指定的元素是否可见。
要格外注意的是visibility_of_element_located()中只能传入一个函数所以一般使用By.XPATHxpath源码
class visibility_of_element_located(object): An expectation for checking that an element is present on the DOM of apage and visible. Visibility means that the element is not only displayedbut also has a height and width that is greater than 0.locator - used to find the elementreturns the WebElement once it is located and visibledef __init__(self, locator):self.locator locatordef __call__(self, driver):try:return _element_if_visible(_find_element(driver, self.locator))except StaleElementReferenceException:return False
再来看一下滚轮滑动的几种用法 1.滑动到指定元素顶端显示 driver.execute_script(argument[0].scrollIntoView();,driver.find_element_by_xpath(xpath)) 2.滑动到指定元素底端显示
driver.execute_script(argument[0].scrollIntoView(false);,driver.find_element_by_xpath(xpath))
3.页面滑动到底端
driver.execute_script(windows.scrollto(0,document.body.scrollHeight)) 4.页面滑动到顶端
driver.execute_script(windows.scrollto(document.body.scrollHeight))
其中包含指定元素的使用时只用注意scrollIntoView后是否跟随false有则底端、无则顶端其次页面滑动时只用注意最后是否从0开始从0开始则为底端、无则顶端 顺带说一下driver.execute_script()这个函数这个函数括号内主要跟随js语句 用法例如
driver.execute_script(argument[0].scrollIntoView(false);var a argument[1],driver.find_element_by_xpath(xpath),num1)
# 其带入的参数没有数量限制只是语法必须使用js语法 感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走 这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取