ole*_*ner 17 python selenium xpath
我正在努力写出自己的预期条件.我需要什么...我有一个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
Run Code Online (Sandbox Code Playgroud)
后来在我的代码中:
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
Run Code Online (Sandbox Code Playgroud)
此代码有效,但多次返回相同的URL.问题是,当我取消注释url_changed_condition它TimeoutException在
(current_url, self.driver.find_element(By.XPATH, u"//a/img").get_attribute(u"src"))
下面的行中它工作正常...我不明白.
ale*_*cxe 16
看起来这个主题错过了自定义预期条件的示例.
这实际上非常简单.首先,Python selenium绑定中的预期条件是什么:
class(基于object)__call__()定义的魔术方法,它返回一个布尔值有一大组内置的预期条件类.
让我们通过实例来解决.假设我们要等到元素的文本以所需文本开头:
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
Run Code Online (Sandbox Code Playgroud)
用法:
WebDriverWait(driver, 10).until(wait_for_text_to_start_with((By.ID, 'myid'), "Hello, World!"))
Run Code Online (Sandbox Code Playgroud)
根据文档:
WebDriverWait 默认情况下每 500 毫秒调用一次 ExpectedCondition,直到成功返回。对于 ExpectedCondition 类型,成功返回值为 Boolean,返回 true;对于所有其他 ExpectedCondition 类型,返回值不为 null。
如果您注释掉自定义等待,您会多次获得相同的 URL,这一事实应该会给您一个提示。
因为 URL 永远不会改变,所以你__call__()总是会返回。False由于您要返回False,因此永远不会满足 ExpectedCondition,并且您会得到TimeoutException.
因此,要么重新定义 ExpectedCondition 逻辑,要么测试不同的情况。
| 归档时间: |
|
| 查看次数: |
10578 次 |
| 最近记录: |