我如何使用EC.Presence_of_element_located在硒的子元素(相对元素)的xpath搜索?<



我想使用EC.presence_of_element_located来检查按钮是否存在,所以我尝试了这个:

def _check_button_is_present(driver: WebDriver, row: WebElement) -> bool:
"""
Check button is present
"""
ELEMENT_ID = 'myButtonPartialId'
try:
element: WebElement = WebDriverWait(driver, timeout=30).until(
EC.visibility_of_element_located(
(   
By.XPATH,
row.find_element_by_xpath(f'.//button[contains(@id, "{ELEMENT_ID}")]')
)
)
)
logger.debug(f'Button found')
except TimeoutException:
logger.error(f'Element "{ELEMENT_ID}" could not be found')
return False
return True

但是这个方法需要一个定位符,所以改成:

EC.visibility_of_element_located(
row.find_element_by_xpath(f'.//button[contains(@id, "{ELEMENT_ID}")]')
)

不会使技巧,例如等待,直到元素是可点击的。

如何将元素的相对xpath传递给visibility_of_element_located?

如果我没理解错的话,你需要

WebElement = WebDriverWait(row, timeout=30).until(
EC.visibility_of_element_located(
(   
By.XPATH,
f'.//button[contains(@id, "{ELEMENT_ID}")]'
)
)

注意你需要传递row而不是driver作为参数给WebDriverWait

最新更新