python - 在 Firefox 中继续使用 selenium 之前等待类存在

标签 python python-2.7 selenium

我试图让 selenium 等到某个类可以在页面上找到,我已经尝试了几段代码但没有任何效果

尝试以下操作:

while not firefox.find_element_by_css_selector('.but selected'):
    print "test"
    time.sleep(1)

返回

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element

尝试以下操作:

while not firefox.find_element_by_class_name('but selected'):
    print "test"
    time.sleep(1)

返回:

selenium.common.exceptions.InvalidSelectorException: Message: The given selector but selected is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Compound class names not permitted

知道我做错了什么以及如何解决吗?

最佳答案

你可以试试 explicit-waits .这是一个小例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


def test(url):
    wait_for_element = 30  # wait timeout in seconds
    firefox = webdriver.Firefox()
    firefox.get(url)

    try:
        WebDriverWait(firefox, wait_for_element).until(
            EC.element_to_be_clickable((By.CLASS_NAME, "but selected'")))
    except TimeoutException as e:
        print("Wait Timed out")
        print(e)

if __name__ == '__main__':
    test("http://www.python.org")

关于python - 在 Firefox 中继续使用 selenium 之前等待类存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338330/

相关文章:

python - 合并两个数据框pandas

python - 为什么多处理中的扩展进程比所有其他进程都慢&

python - 如何避免使用 python 覆盖 excel 中的单元格?

java - 从终端执行的命令,使用 ProcessBuilder 从 Java 失败

python - 使用 Selenium 为不停止加载的页面拍摄快照

python - 使用 Visual Studio 在 IronPython 中引用库

javascript - 从javascript返回值到pyqt5

python - 为什么在这种情况下要追加 list.extend ?

python - 如何按字母顺序加入Python中的多个排序文件?

java - 如何在不使用 for 循环的情况下转储 <WebElement> 列表的内容