简体   繁体   English

如何使用 Selenium 更改 div 元素的文本

[英]How to change the text of a div element using Selenium

In the following HTML:在以下 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.我想使用 python selenium 将其更改为Client_2_name而不是Client_1_name Is there any way to do it?有什么办法吗?

To change the innerText of the <div> element更改<div>元素的innerText

<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:Client_1_nameClient_2_name您可以使用以下任一解决方案:

  • Using innerText :使用innerText

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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