【问题标题】:How to send HTML from Selenium using sendKeys如何使用 sendKeys 从 Selenium 发送 HTML
【发布时间】:2021-06-24 16:42:22
【问题描述】:

我有一个使用 -

制作的 HTML 代码
find_all(["h2", "h3", "h4", "h5", "ul", "p", "h6"])

所以,它的代码如下 -

<p>Hello this is a paragraph</p>
<h2>This is a Sub Heading</h2>

我想使用发送键将整个 HTML 发送到我的 Wordpress 帖子,但它返回错误

return self.attrs[key] KeyError: 0

如果我使用 .text 或 .contents,我可以发送,但它会删除 HTML 标记。我想发送它们包括标签,我该怎么做?

【问题讨论】:

    标签: python html wordpress selenium automation


    【解决方案1】:

    在这里试试这个代码:

    from seleniumwire import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    browser = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
    browser.get('https://www.google.com')
    search = browser.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
    html_text = """
    <p>Hello this is a paragraph</p>
    <h2>This is a Sub Heading</h2>
    """
    
    search.send_keys(html_text)
    

    也许问题出在文本处理上

    【讨论】:

    • 我从一个网站上废弃了 HTML,所以如果我使用 f"""{scrapped_html}""" 它应该可以工作。谢谢,但是我发现了另一个效果很好的答案,即简单地将其转换为字符串...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多