Python Selenium WebDriver.编写我自己的预期条件 [英] Python Selenium WebDriver. Writing my own expected condition

查看:53
本文介绍了Python Selenium WebDriver.编写我自己的预期条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写自己的预期条件.我需要什么...我有一个 iframe.我也有一个图像.当图像的 scr 发生变化时,我想继续处理.我做了什么:

I'm trying to write my own expected condition. What I need... I have an iframe. and I also have an image in it. I want to continue processing when image's scr will change. What I did:

class url_changed_condition(object):
    '''
    Checks whether url in iframe has changed or not
    '''
    def __init__(self, urls):
        self._current_url, self._new_url = urls

    def __call__(self, ignored):
        return self._current_url != self._new_url  

然后在我的代码中:

def process_image(self, locator, current_url):
    try:
        WebDriverWait(self.driver, 10).until(ec.presence_of_element_located((By.TAG_NAME, u"iframe")))
        iframe = self.driver.find_element(*locator)
        if iframe:
            print "Iframe found!"
        self.driver.switch_to_frame(iframe)
        WebDriverWait(self.driver, 10).until(ec.presence_of_element_located((By.XPATH, u"//div")))

        # WebDriverWait(self.driver, 10).until(
            # url_changed_condition(
                # (current_url, self.driver.find_element(By.XPATH, u"//a/img").get_attribute(u"src"))))

        img_url = self.driver.find_element(By.XPATH, u"//a/img").get_attribute(u"src")
        print img_url
        self.search_dict[self._search_item].append(img_url)
        self.driver.switch_to_default_content()
    except NoSuchElementException as NSE:
        print "iframe not found! {0}".format(NSE.msg)
    except:
        print "something went wrong"
        import traceback
        import sys
        type_, value_, trace_ = sys.exc_info()
        print type_, value_
        print traceback.format_tb(trace_)
    finally:
        return current_url  

此代码有效,但多次返回相同的网址.问题是当我取消注释 url_changed_condition 时,它落在 TimeoutException in
(current_url, self.driver.find_element(By.XPATH, u"//a/img").get_attribute(u"src"))
它下面的行工作正常......我不明白.

This code works, but returns the same url multiple times. The problem is when I uncomment url_changed_condition it falls with TimeoutException in
(current_url, self.driver.find_element(By.XPATH, u"//a/img").get_attribute(u"src"))
The line below it works fine... I don't get it.

推荐答案

看起来这个主题缺少一个自定义预期条件的例子.

Looks like this topic misses an example of a Custom Expected Condition.

其实很简单.首先,什么是 Python selenium 绑定中的预期条件:

It is actually pretty easy. First of all, what is an Expected Condition in Python selenium bindings:

  • it is a new-style class (based on object)
  • it has __call__() magic method defined which returns a boolean

有一大组内置-在预期条件类别.

让我们通过示例来工作.假设我们要等到元素的文本以所需文本开头:

Let's work through example. Let's say we want to wait until an element's text will start with a desired text:

from selenium.webdriver.support import expected_conditions as EC

class wait_for_text_to_start_with(object):
    def __init__(self, locator, text_):
        self.locator = locator
        self.text = text_

    def __call__(self, driver):
        try:
            element_text = EC._find_element(driver, self.locator).text
            return element_text.startswith(self.text)
        except StaleElementReferenceException:
            return False

用法:

WebDriverWait(driver, 10).until(wait_for_text_to_start_with((By.ID, 'myid'), "Hello, World!"))

这篇关于Python Selenium WebDriver.编写我自己的预期条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆