如何删除 selenium python 中的元素

sti*_*kie 3 python selenium

我想知道如何删除selenium python中的元素,我想删除网站中的聊天框,就像您通过按退格键手动删除一样,但我想在selenium中删除它。到目前为止我已经尝试过这个

chatBox = driver.find_element(By.XPATH, "//div[@class='chatContainer oldStyle']").remove()
Run Code Online (Sandbox Code Playgroud)

这行代码给出了错误

AttributeError: 'WebElement' object has no attribute 'remove'
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激

sti*_*kie 7

我找到了解决方案,这是我用来修复它的代码

        try:
            element = driver.find_element_by_xpath("//div[@class='chatContainer oldStyle']")
        driver.execute_script("""var element = arguments[0]; 
            element.parentNode.removeChild(element);""", element)
        except Exception:
            pass
Run Code Online (Sandbox Code Playgroud)