How can I edit an element in a browser that I opened with Python 2.7 Selenium. Let's say I have this element:
<span id="some-random-number">100</span>
I know that you can find the text with:
driver.find_element_by_id("some-random-number").text
but I want the text of the element change to let say 200 so:
<span id="some-random-number">200</span>
How can I do it? Also except from text can I also edit element's attribute like for example this:
<div class="something" style="transition: transform 1000ms bla bla bla"></div>
Can my script read the attribute and edit the 1000 ms into different value?
Here is your answer -
Change the text of element -
driver.execute_script("document.getElementById('some-random-number').innerHTML = '200';");
or
element =driver.find_element_by_id("some-random-number")
driver.execute_script("arguments[0].innerText = '200'", element)
Change or set an attribute value
element = driver.find_element_by_class_name("something");
driver.execute_script("arguments[0].setAttribute('style', 'transition: transform 2500ms bla bla bla')", element);
Try this:
element =driver.find_element_by_id("some-random-number")
driver.execute_script("arguments[0].innerText = '200'", element)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With