简体   繁体   中英

How do I remove an element in selenium python

I was wondering how to remove an element in selenium python, I'm wanting to remove a chatBox in a website like how you would if you manually did it by pressing backspace, but I want to do it in selenium. So far I've tried this

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'

Any help would be much appreciated

You can use javascript to remove the element.

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

I found a solution to this, this is the code I used to fix it

        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM