1

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>
4
  • Are you positive the xpath is correct? It looks pretty brittle. Commented Mar 1, 2021 at 12:08
  • what does brittle mean? I am right clicking and copying full xpath. Commented Mar 1, 2021 at 12:11
  • @Zanam : what error are you getting? Commented Mar 1, 2021 at 12:21
  • @Zanam Brittle means it breaks easily. You copied full xpath but next time you run the test xpath might be slightly different so selenium will not be able to find the element. Commented Mar 1, 2021 at 13:08

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.