Zanam
Zanam

Reputation: 4807

Python selenium getting text from text area

I am trying to copy the text area message into a variable so that I can parse it.

The text area has message as follows:

<?xml version='1.0' encoding='UTF-8'?><Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body><SubmitResponse xmlns="http://eftr</Error></SubmitResponse></Body></Envelope>

The HTML is as follows:

<textarea class="v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly" rows="5" tabindex="-1" readonly="" style="width: 100%; height: 100%;"></textarea>

I am trying the following and other configurations but I have not success:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
full_xpath = r'/html/body/div[1]/div/div[2]/div/div[2]/div/div[2]/div/div/div[3]/div/div/div[2]/div/div[2]/div/div/div/div/div[3]/textarea'
path_to_chromedriver = r'C:\chromedriver'  # change path as needed
browser = webdriver.Chrome(executable_path=path_to_chromedriver)
.........
.........
content = browser.find_element_by_xpath(full_xpath).text

I am not sure how to achieve the above.

Basically print(content) should produce the following:

<?xml version='1.0' encoding='UTF-8'?><Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body><SubmitResponse xmlns="http://eftr</Error></SubmitResponse></Body></Envelope>

Upvotes: 1

Views: 733

Answers (2)

PDHide
PDHide

Reputation: 19929

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

textcontent=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//textarea[@class='v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly']"))).get_attribute("value")

in textarea text is stored as value,

an example text area:

<!DOCTYPE html>
<html>
<body>

<h1>The textarea element</h1>

<form action="/action_page.php">
<label for="w3review">Review of W3Schools:</label>
<textarea id="w3review" name="w3review" rows="4" cols="50">
  At w3schools.com you will learn how to make a website. They offer free tutorials in all web development technologies.
  </textarea>
  <br><br>
  <input type="submit" value="Submit">
</form>

<p>Click the "Submit" button and the form-data will be sent to a page on the 
server called "action_page.php".</p>
</body>
</html>

you can use above example html and in console try

   $('textarea').value

Upvotes: 1

KunduK
KunduK

Reputation: 33384

use following xpath to idetify the element.

content = browser.find_element_by_xpath("//textarea[@class='v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly']").text

Or use get_attribute("innerHTML")

content = browser.find_element_by_xpath("//textarea[@class='v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly']").get_attribute("innerHTML")

I would suggest use WebDriverWait() and wait for visibility_of_element_located()

content=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//textarea[@class='v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly']"))).text

you need to import below libraries

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 0

Related Questions