我有一个简单的页面,有几个标签。它使用棱角。
在初始页面加载时,元素加载,然后在数据加载时,我在禁用用户活动的所有内容之上都有一个元素,直到数据加载。
脚本有一个"等到找到元素的可见性",当我使用 chromedriver 运行它时,它会起作用。
当我添加"无头"选项时,测试失败,因为它找不到该元素。我可以做些什么来在我的页面上工作并且无头,或者我必须使用 GUI 坚持下去?
失败的代码:
def loading_appear_disappear(self):
element = BaseLocators.Locators.OVERLAY_GRID
wait = WebDriverWait(self.driver, 10)
wait.until(EC.visibility_of_element_located(element))
wait.until(EC.invisibility_of_element_located(element))
如果删除等待,则应获取选项卡列表的代码不会返回任何元素
driver.find_elements(*BaseLocators.Locators.TAB_ELEMENTS)
由于我们需要等待Tab Elements
列表,
而不是:
element = BaseLocators.Locators.OVERLAY_GRID
wait = WebDriverWait(self.driver, 10)
wait.until(EC.visibility_of_element_located(element))
wait.until(EC.invisibility_of_element_located(element))
尝试使用 :
wait = WebDriverWait(self.driver, 10)
wait.until(EC.presence_of_all_elements_located(*BaseLocators.Locators.TAB_ELEMENTS))
更新:
正如你提到的,elements are already 'loaded' but inaccessible due to the overlay grid
,所以最好的使用子句是element_to_be_clickable
。在这种情况下,您可能需要将任何一个TAB_ELEMENTS
定义/声明为constant
例如 FIRST_TAB_ELEMENT
并在wait
中使用它
wait = WebDriverWait(self.driver, 10)
wait.until(EC.element_to_be_clickable(*BaseLocators.Locators.FIRST_TAB_ELEMENT))
我能够通过以下方式解决此问题:grid_element实际上是被条形图阻挡的网格。我的猜测是,我试图在基本页面完成加载之前"等待",所以它搞砸了。
try:
if grid_element.is_displayed and grid_element.is_enabled:
self.wait.until(EC.visibility_of_element_located(bar))
self.wait.until(EC.invisibility_of_element_located(bar))
except WebDriverException:
#error because 'another element would receive the click' meaning the overlay bar is present
self.wait.until(EC.invisibility_of_element_located(bar))