繁体   English   中英

Python Selenium WebDriver。 写我自己的预期条件

[英]Python Selenium WebDriver. Writing my own expected condition

我正在努力写出自己的预期条件。 我需要什么...我有一个iframe。 我也有一个图像。 我想在图像的scr改变时继续处理。 我做了什么:

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。 问题是,当我取消注释url_changed_condition它会因TimeoutExceptionurl_changed_condition
(current_url, self.driver.find_element(By.XPATH, u"//a/img").get_attribute(u"src"))
它下面的一行很好......我不明白。

看起来这个主题错过了自定义预期条件的示例。

这实际上非常简单。 首先, Python selenium绑定中预期条件是什么:

有一大组内置的预期条件类

让我们通过实例来解决。 假设我们要等到元素的文本以所需文本开头

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!"))

根据文件

默认情况下,WebDriverWait每500毫秒调用一次ExpectedCondition,直到它成功返回。 对于所有其他ExpectedCondition类型,ExpectedCondition类型的布尔返回true或非null返回值成功返回。

如果您注释掉自定义等待,您多次获得相同的URL这一事实应该会给您一个提示。

__call__()您始终返回False因为URL永远不会更改。 由于您返回False ,因此永远不会遇到ExpectedCondition并且您获得TimeoutException

因此要么重新定义ExpectedCondition逻辑,要么测试不同的情况。

使用@alecxe描述的技术,但略微修改以应对接受元素而不是定位器的expected_conditions :(在这种情况下,因为没有selenium.webdriver.support.expected_conditions.invisibility_of(element) ,我是等待is_displayed()方法发出False信号)

class wait_for_element_to_be_invisible(object):
    def __init__(self, element):
        self.element = element

    def __call__(self, driver):
        return not(self.element.is_displayed())

def test_collapsible_blocks_expand_or_collapse(self):
    self.browser.get(self.server_url+'/courses/1/')
    shadables = self.browser.find_elements_by_class_name('shade')
    for shadable in shadables:
        ## parent, then sibling element (*)
        shady_bit = shadable.find_element_by_xpath('../following-sibling::*')
        element = WebDriverWait(self.browser, 10).until(
            EC.visibility_of(shady_bit))
        shadable.click()
        element = WebDriverWait(self.browser, 10).until(
            self.wait_for_element_to_be_invisible(shady_bit))

带有DOM相关位的HTML片段是:

<h4 class="shadable"><span class="shade" title="Show/Hide">↓</span>
  <a href="/link/to/uri">Title of Section</a>
</h4>
<article class="abstract">
  <p>Descriptive part which is shadable
  </p>
</article>`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM