playwright浅试

  • 打开浏览器页面登录,结束后保存登录文件cway
python -m playwright cr http://gitlab.com:1180 --save-storage cway
  • 使用登录文件,录制执行动作生成代码
python -m playwright codegen --target python -o 'login.py' http://gitlab.com:1180/demo001/-/settings/repository#js-push-remote-settings --load-storage cway
  • 使用xpath定位元素,并执行点击
    locator = page.locator("xpath=//div[@class='form-group js-ssh-host-keys-section collapse']")
    locator.click()
  • 等待元素变为可见状态
    page.wait_for_selector(r"xpath=//*[@class='gl-show-field-errors js-mirror-form']/div/div[1]/div[5]/div[1]", state="visible", timeout=5*1000 )
  • 元素触发一个事件
repo_url_ele.dispatch_event('keyup')
# 更多事件参考这个网页
# https://developer.mozilla.org/en-US/docs/Web/API/Event/Event
  • 改变元素属性/执行js指令
    # 改变该元素为可见
    page.eval_on_selector("xpath=//div[@class='form-group js-ssh-host-keys-section collapse']", "el => el.setAttribute('class','form-group js-ssh-host-keys-section collapse show')")
posted @ 2022-08-25 15:51  小小记录本  阅读(137)  评论(0编辑  收藏  举报