相关疑难解决方法(0)

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

我正在努力写出自己的预期条件.我需要什么...我有一个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)

python selenium xpath

17
推荐指数
2
解决办法
1万
查看次数

如何使用 Selenium 和 Python 调用函数之前等待特定 URL 加载

这是我第一次在 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)

python url selenium webdriver webdriverwait

6
推荐指数
1
解决办法
6700
查看次数

标签 统计

python ×2

selenium ×2

url ×1

webdriver ×1

webdriverwait ×1

xpath ×1