【问题标题】:Changing display property for a hidden text area element with Playwright in Python在 Python 中使用 Playwright 更改隐藏文本区域元素的显示属性
【发布时间】:2022-02-11 00:41:00
【问题描述】:

我正在尝试将剪贴板中的一些文本粘贴到具有 Playwright 的网站上的隐藏文本区域输入元素中,但由于该元素具有属性,我不断遇到问题:

style="visibility:hidden, display:none;"

我正在尝试使用 Playwright 中的 page.evaluate 命令解决此问题,但似乎无法更改元素可见性状态。这是返回的错误:

page.evaluate("[id=txbLongDescription] => document.querySelector('[id=txbLongDescription]')", style='visibility:visible')
TypeError: Page.evaluate() got an unexpected keyword argument 'style'

到目前为止,这是我的代码:

def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(accept_downloads=True)
page = context.new_page()


# Click in description input field


#paste description from clipboard
paste = pc.paste()


page.evaluate("[id=txbLongDescription] => document.querySelector('[id=txbLongDescription]')", style='visibility:visible')

page.fill('textarea[id=\"txbLongDescription\"]', f'{paste}')


#---------------------
context.close()
browser.close()

with sync_playwright() as playwright:
    run(playwright)
print('Done')

【问题讨论】:

  • 评估不是有效的 javascript 代码。
  • 你能引导我走向正确的方向吗?这是剧作家的事还是别的什么?

标签: python playwright-python


【解决方案1】:

这是我的解决方案:

page.eval_on_selector(
  selector="textarea", # Modify the selector to fit yours.
  expression="(el) => el.style.display = 'inline-block'",
)

Hereeval_on_selector()的文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    相关资源
    最近更新 更多