0

I am trying to extract message from alert message in selenium python

<div ng-hide="authorizationMessage">
<div class="ng-valid-maxlength ng-dirty ng-valid-parse ng-valid ng-valid-required" ng-form="cohortForm">
<div class="alert alert-success ng-binding" role="alert" ng-show="message">
<a class="close" href="" data-dismiss="alert" aria-label="close">×</a>
  **Cohort definition is created successfully.**
</div>

I tried doing

var1 = driver.find_element_by_xpath("html/body/div[1]/div/div/div[3]/div/div[1]").get_attribute('text')
print var1

But it printed None.

How should I extract message - "Cohort definition is created successfully." from above HTML page?

0

2 Answers 2

1

get_attribute('text') it's not what you actually need to use to get text from target div element

You can simply use text property or get textContent attribute as below:

print(driver.find_element_by_css_selector('div.alert.alert-success.ng-binding').text)
print(driver.find_element_by_css_selector('div.alert.alert-success.ng-binding').get_attribute('textContent'))
Sign up to request clarification or add additional context in comments.

4 Comments

Because the A tag is also contained in the target DIV, you will get an X at the beginning of that string but there's not really much you can do about that short of removing it, etc.
ok... print(driver.find_element_by_css_selector('div.alert.alert-success.ng-binding').get_attribute('textContent').replace('\xd7', '')) should allow to get rid of "x"
It wasn't a complaint, it was merely an FYI to OP because he's likely to see this seemingly random X show up in the returned text.
Thank you Andersson and JeffC , I tried this solution. But it resulted in printing 'x' not the message "Cohort definition is created successfully."
1

To extract notification message values from selenium, paste the .text after them.

This is an example code for you.

var1 = driver.find_element_by_xpath("html/body/div[1]/div/div/div[3]/div/div[1]").text

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.