python - 如何在不使用机器人框架和 python 滚动的情况下获取所有匹配元素?

标签 python selenium xpath automated-tests robotframework

我需要获取所有匹配 xpath 的元素,而不需要滚动到所有元素,因为我永远不知道是否真的需要滚动(取决于显示器分辨率,列宽也可以由开发人员更改)而且我不知道滚动到哪里,因为我正在检查列的存在,在某些情况下,正确的结果也是该列不存在......

我在测试中使用这些函数来获取列标题列表:

Get all columns of table ${table}
    ${columns_loc}=  replace substring placeholder ${GRID_HEADERS}  ${table}
    @{locators}=   Get Webelements    ${columns_loc}
    ${result}=       Create List
    :FOR   ${locator}   in    @{locators}
    \       ${name}=    Get Text    ${locator}
    \       Append To List  ${result}  ${name}
    [Return]   ${result}

Check column ${column} is visible
   ${headers2}=  Get all values
   ${res}=  value is in list   ${headers2}   ${column}
   run keyword if    ${res}==False  fail  wrong columns displayed
...                  else  pass execution

我也试过用这个:

def dict_elements(self, xpath):
    elements = self.get_library_instance()._element_find(xpath, False, True)
    headers_map = {}
    if elements is not None:
        for index, element in enumerate(elements):
            headers_map[element.text] = index
        return headers_map
    return None

def list_elements(self, xpath):
    dict = self.dict_elements(xpath)
    return dictionary_keys_to_list(dict)

Get all columns of table ${table}
    ${columns_loc}=  replace substring placeholder ${GRID_HEADERS}  ${table}
    @{cols}=  list elements  ${columns_loc}

但是两个变体都返回 9 列 - 这是正确的,实际上有 9 列,但最后一列是 EMPTY(仅返回 u'')而不是元素的实际文本(如 u'last column name').最后一列标题不直接可见,必须水平滚动才能找到它... 表格由angular动态生成。

感谢您的任何建议!

最佳答案

您可以使用 Get Matching Xpath Count 引用 Get Matching Xpath Count文档中的关键字以获取更多信息。

关于python - 如何在不使用机器人框架和 python 滚动的情况下获取所有匹配元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43938349/

相关文章:

python - Selenium python递归文本抓取

selenium - CSS选择器-如何定位父元素

python - 如何在复杂的情况下获得假人

python - hash() 在不同的操作系统上返回不同的值

python - 有没有办法控制pygame中矩形的大小?

java - 如何处理 selenium webdriver 中的弹出窗口(不警报)?

python - 属性错误: 'NoneType' object has no attribute 'save'

python - 如何为在 Python 中运行的 Selenium 选择自定义端口

python - selenium.common.exceptions.ElementNotVisibleException : Message: element not visible while invoking send_keys in ubuntu headless browser through python

java - Selenium Java 无法选择 AngularJS 元素