Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit element in browser with python selenium

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?

like image 724
Programer Beginner Avatar asked Jan 09 '17 17:01

Programer Beginner


2 Answers

Here is your answer -

  1. 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)
    
  2. 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);
    
like image 105
NarendraR Avatar answered Oct 04 '22 02:10

NarendraR


Try this:

element =driver.find_element_by_id("some-random-number")
driver.execute_script("arguments[0].innerText = '200'", element)
like image 30
Shijo Avatar answered Oct 04 '22 02:10

Shijo