我正在努力写出自己的预期条件.我需要什么...我有一个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" …Run Code Online (Sandbox Code Playgroud) 这是我第一次在 stackoverflow 上发帖,我对 Selenium 和 Python 还有些陌生。
当 URL 等于 fx: https://www.example.com时,我不希望运行函数。
我在另一个讨论中读过 这个答案,但我不太明白发生了什么。
我希望您能抽出时间回答我的问题。
好的,所以我刚刚尝试过这个:
driver.get('https://www.google.com')
time.sleep(4)
driver.get('https://www.stackoverflow.com')
if WebDriverWait(driver, 10).until(EC.url_to_be('https://stackoverflow.com')):
print('Desired url was rendered within allocated time')
else:
print('Desired url was not rendered within allocated time')
Run Code Online (Sandbox Code Playgroud)
但这没有用。有任何想法吗?
控制台说
Traceback (most recent call last):
File "/Users/holger/PycharmProjects/waitTest/wait.py", line 15, in <module>
if WebDriverWait(browser, 10).until(EC.url_to_be('https://www.stackoverflow.com')):
File "/Users/holger/PycharmProjects/waitTest/venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Run Code Online (Sandbox Code Playgroud)