user2661518
user2661518

Reputation: 2745

selenium python delete text between xml tags

I am trying to delete text between tags but is there better way than below

<p>test_line</p>

currently I am using action chains

action_chains = ActionChains(driver)
.send_keys(Keys.BACKSPACE).send_keys(Keys.BACKSPACE)

So I'm doing above for 9 times. I see option Keys.DELETE but it's same thing

Upvotes: 0

Views: 440

Answers (2)

Rahul Kote
Rahul Kote

Reputation: 81

Have you tried achieving that with the help of execute_script

so in js to remove text or inner html you can do following

e.g document.getElementsByTagName('p').innerHTML = '';

and to do above thing in selenium python you can try following

driver.execute_script("document.getElementsByTagName('p').innerHTML = '';")

Upvotes: 1

alecxe
alecxe

Reputation: 473873

Aside from using .clear() which would only clear out the value attribute by definition and does not work for your use case, you can improve your current approach and build your action chains dynamically:

actions = ActionChains(driver)
for _ in range(9):
    actions = actions.send_keys(Keys.BACKSPACE)
actions.perform()

Or, even simpler:

actions = ActionChains(driver)
actions.send_keys(Keys.BACKSPACE * 9).perform()

Note that this is still a single selenium command issued by perform().

Upvotes: 1

Related Questions