我试图让 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/