Selenium 等到所有指定的 Web 元素都更改或消失
Selenium wait until all specified web elements are changed or gone
假设有一个HTML块
<div id='list'>
<p>hello</p>
<div class='locked'>world</div>
<p>你好</p>
<div class='locked'>世界</div>
</div>
如何将 selenium 与 python 一起使用等待一段时间,直到 所有 <div class='locked'>
标签变成其他东西。(例如 <div class='unlock'>xxx</div>
谢谢!
可以编写自定义预期条件来检查您需要达到的特定状态。就像在您的场景中一样,您希望具有锁定状态或 class 的控件处于计数 0 以继续进行。我已经给出了可用于自定义同步功能的代码示例
我是基于 python 的 Selenium 的新手,但它认为这应该可以解决问题,假设您使用 xpath 来识别锁定状态控件。
-------------更新为 Python 代码-------------------- ----
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class WaitForLockStateChange:
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
return len(driver.find_elements_by_xpath(self.locator)) == 0
wait = WebDriverWait(driver, 10)
element = wait.until(WaitForLockStateChange("//div[@class='locked']"))
我有一个定制的 class 我为一个非常相似的目的而创建(在我的例子中,我对 "value" 属性 变化感兴趣,但我修改它以适应您的 "class" 更改示例):
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class WaitForAttrValueChange:
def __init__(self, locator, val_):
self.locator = locator
self.val = val_
def __call__(self, driver):
try:
attr_value = EC._find_element(driver, self.locator).get_property('className')
return attr_value.startswith(self.val)
except SE.StaleElementReferenceException:
return False
然后你可以用WebDriverWait
来使用它(显然你可以使用任何By
识别方法而不是By.ID
,这只是一个例子):
WebDriverWait(driver, 20).until(WaitForAttrValueChange((By.ID, 'id'), 'locked'))
使用 with python to wait for all the <div class='locked'>
tags become <div class='unlock'>xxx</div>
you have to induce WebDriverWait for the visibility_of_all_elements_located()
and you can use either of the following :
使用CSS_SELECTOR
:
WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div#list div.unlock")))
使用XPATH
:
WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='list']//div[@class='unlock']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
假设有一个HTML块
<div id='list'>
<p>hello</p>
<div class='locked'>world</div>
<p>你好</p>
<div class='locked'>世界</div>
</div>
如何将 selenium 与 python 一起使用等待一段时间,直到 所有 <div class='locked'>
标签变成其他东西。(例如 <div class='unlock'>xxx</div>
谢谢!
可以编写自定义预期条件来检查您需要达到的特定状态。就像在您的场景中一样,您希望具有锁定状态或 class 的控件处于计数 0 以继续进行。我已经给出了可用于自定义同步功能的代码示例 我是基于 python 的 Selenium 的新手,但它认为这应该可以解决问题,假设您使用 xpath 来识别锁定状态控件。
-------------更新为 Python 代码-------------------- ----
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class WaitForLockStateChange:
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
return len(driver.find_elements_by_xpath(self.locator)) == 0
wait = WebDriverWait(driver, 10)
element = wait.until(WaitForLockStateChange("//div[@class='locked']"))
我有一个定制的 class 我为一个非常相似的目的而创建(在我的例子中,我对 "value" 属性 变化感兴趣,但我修改它以适应您的 "class" 更改示例):
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class WaitForAttrValueChange:
def __init__(self, locator, val_):
self.locator = locator
self.val = val_
def __call__(self, driver):
try:
attr_value = EC._find_element(driver, self.locator).get_property('className')
return attr_value.startswith(self.val)
except SE.StaleElementReferenceException:
return False
然后你可以用WebDriverWait
来使用它(显然你可以使用任何By
识别方法而不是By.ID
,这只是一个例子):
WebDriverWait(driver, 20).until(WaitForAttrValueChange((By.ID, 'id'), 'locked'))
使用<div class='locked'>
tags become <div class='unlock'>xxx</div>
you have to induce WebDriverWait for the visibility_of_all_elements_located()
and you can use either of the following
使用
CSS_SELECTOR
:WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div#list div.unlock")))
使用
XPATH
:WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='list']//div[@class='unlock']")))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC