如何在Selenium python中删除一个元素

2 人关注

我想知道如何在Selenium python中删除一个元素,我想在一个网站中删除一个聊天框,就像你手动按退格键那样,但我想在Selenium中这样做。到目前为止,我已经尝试了以下方法

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

This line of code gives the error

AttributeError: 'WebElement' object has no attribute 'remove'

如果有任何帮助,我们将非常感激

python
selenium
stinkiepinkie
stinkiepinkie
发布于 2021-12-04
2 个回答
stinkiepinkie
stinkiepinkie
发布于 2021-12-04
已采纳
0 人赞同

我找到了一个解决方案,这是我用来解决这个问题的代码

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