简体   繁体   中英

How to change the text of a div element using Selenium

In the following HTML:

<div class="stylesSelectDropdown__dropdown-label-single____d17A">**Client_1_name**</div>

Instead of Client_1_name I want to change it to Client_2_name using python selenium. Is there any way to do it?

To change the innerText of the <div> element

<div class="stylesSelectDropdown__dropdown-label-single____d17A">Client_1_name</div>

from Client_1_name to Client_2_name you can use either of the following solutions:

  • Using innerText :

     element = driver.find_element(By.XPATH, "//div[text()='Client_1_name']") driver.execute_script("arguments[0].innerText = 'Client_2_name'", element)
  • Using setAttribute() :

     element = driver.find_element(By.XPATH, "//div[text()='Client_1_name']") driver.execute_script("arguments[0].setAttribute('innerHTML','Client_2_name')", element)

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