我应该如何编写Selenium函数来等待仅具有类标识符的表格?使用Python时,我非常困难地学习使用Selenium的Python Webdriver函数。
我应该如何编写Selenium函数来等待仅具有类标识符的表格?使用Python时,我非常困难地学习使用Selenium的Python Webdriver函数。
import contextlib
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
with contextlib.closing(webdriver.Firefox()) as driver:
driver.get('http://www.google.com')
wait = ui.WebDriverWait(driver,10)
# Do not call `implicitly_wait` if using `WebDriverWait`.
# It magnifies the timeout.
# driver.implicitly_wait(10)
inputElement=driver.find_element_by_name('q')
inputElement.send_keys('Cheese!')
inputElement.submit()
print(driver.title)
wait.until(lambda driver: driver.title.lower().startswith('cheese!'))
print(driver.title)
# This raises
# selenium.common.exceptions.TimeoutException: Message: None
# after 10 seconds
wait.until(lambda driver: driver.find_element_by_id('someId'))
print(driver.title)
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import selenium.webdriver.support.expected_conditions as EC
import selenium.webdriver.support.ui as ui
@classmethod
def setUpClass(cls):
cls.selenium = WebDriver()
super(SeleniumTest, cls).setUpClass()
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super(SeleniumTest, cls).tearDownClass()
# return True if element is visible within 2 seconds, otherwise False
def is_visible(self, locator, timeout=2):
try:
ui.WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))
return True
except TimeoutException:
return False
# return True if element is not visible within 2 seconds, otherwise False
def is_not_visible(self, locator, timeout=2):
try:
ui.WebDriverWait(driver, timeout).until_not(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))
return True
except TimeoutException:
return False
然后你可以像下面这样在测试中轻松使用它们:
def test_search_no_city_entered_then_city_selected(self):
sel = self.selenium
sel.get('%s%s' % (self.live_server_url, '/'))
self.is_not_visible('#search-error')
我使用过以下两种方法:
第一个方法很明显 - 只需等待几秒钟即可完成某些操作。
对于所有我的Selenium脚本,当我在笔记本电脑上运行它们时,使用带有几秒钟范围(1到3)的sleep()方法就可以了,但是在我的服务器上等待时间范围更广,所以我也使用implicitly_wait()方法。我通常使用implicitly_wait(30),这已经足够了。
隐式等待是告诉WebDriver,在查找元素或元素时,在DOM中轮询一定的时间,如果它们不立即可用。默认设置为0。一旦设置,隐式等待将设置为WebDriver对象实例的生命周期。
由于Python的Selenium驱动程序不支持wait_for_condition函数,因此我为Python实现了以下内容。
def wait_for_condition(c):
for x in range(1,10):
print "Waiting for ajax: " + c
x = browser.execute_script("return " + c)
if(x):
return
time.sleep(1)
用作
等待 ExtJS Ajax 请求不再挂起:
wait_for_condition("!Ext.Ajax.isLoading()")
wait_for_condition("CG.discovery != undefined;")
etc.
您可以在循环中使用短暂的睡眠,并将其传递给您的元素ID:
def wait_for_element(element):
count = 1
if(self.is_element_present(element)):
if(self.is_visible(element)):
return
else:
time.sleep(.1)
count = count + 1
else:
time.sleep(.1)
count = count + 1
if(count > 300):
print("Element %s not found" % element)
self.stop
#prevents infinite loop
Wait Until Page Contains Element
。例如,考虑以下HTML代码:<body>
<div id="myDiv">
<table class="myTable">
<!-- implementation -->
</table>
</div>
</body>
...您可以输入以下关键字:
Wait Until Page Contains Element //table[@class='myTable'] 5 seconds
除非我漏掉了什么,否则没有必要为此创建一个新函数。
def test_sel(self):
driver = self.driver
for i in range(60):
try:
if self.is_element_present(By.XPATH, "//table[@class='pln']"): break
except: pass
time.sleep(1)
else: self.fail("time out")
from selenium.webdriver.common.by import By
import time
while len(driver.find_elements(By.ID, 'cs-paginate-next'))==0:
time.sleep(100)
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('www.url.com')
try:
wait = driver.WebDriverWait(driver,10).until(EC.presence_of_element_located(By.CLASS_NAME,'x'))
except:
pass