代码之家  ›  专栏  ›  技术社区  ›  Manoj Soundarrajan

分析Playwright中的选择器name=startcreateddate createStackless时未知的引擎“name”

  •  -1
  • Manoj Soundarrajan  · 技术社区  · 2 年前

    我曾试图在Playwright中使用id或name来识别元素,但剧作家抛出了一个错误:

    "Unknown engine "name" while parsing selector name=startcreateddate createStackless"

    error screen

    我的代码是:

    playwright.$("name=startcreateddate") 
    

    如何在Playwright中按Id或名称选择元素?

    1 回复  |  直到 2 年前
        1
  •  1
  •   ggorlen Hoàng Huy Khánh    2 年前

    我猜您正试图选择与以下示例中类似的元素,并在由Playwright Python脚本(基于您的标记)启动的调试会话期间在浏览器控制台中进行。

    用于选择id为的元素:

    <p id="foo">hello</p>
    

    使用 playwright.$("#foo")

    用于选择具有 name= 属性:

    <input name="startcreateddate">
    

    使用 playwright.$('[name="startcreateddate"]')

    控制台中出现奇怪错误的原因是 foo= 语法用于 set the selection engine 喜欢 text= , css= xpath= 名称= 不是有效的引擎选项。

    下面是一个完整的可运行示例(当浏览器控制台暂停在断点上时,您可以将上述命令粘贴到浏览器控制台中):

    from playwright.sync_api import expect, sync_playwright  # 1.37.0
    
    
    html = """<!DOCTYPE html><html><body>
    <p id="foo">hello</p>
    <input name="startcreateddate" value="world">
    </body></html>"""
    
    
    def main():
        with sync_playwright() as p:
            browser = p.chromium.launch(headless=False)
            page = browser.new_page()
            page.set_content(html)
    
            page.pause() # paste the code above into the browser console
    
            # just in case you want to see these selectors in Python...
            p = page.locator("#foo")
            date = page.locator('[name="startcreateddate"]')
    
            print(p.text_content())
            print(date.get_attribute("value"))
    
            expect(p).to_have_text("hello")
            expect(date).to_have_value("world")
    
            browser.close()
    
    
    if __name__ == "__main__":
        main()
    
    推荐文章