Reputation: 3
Here is source html:
<div id="alert_signin" class="alert_modal_error">
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
Invalid username or password.
</div>
</div>
What I really need is to check if message "Invalid username or password." appears (python + selenium webdriver ). But I was unlucky to find it using xpath like
find_element_by_xpath('//div[@id=\'alert_signin\']/div[@class=\'alert\']').text
So I've decided to find exact xpath using message text. I've tried several options like
find_element_by_xpath('//*[text()[contains(.,\'Invalid\')]]')
or
find_element_by_xpath('//*[contains(., \'Invalid username or password.\')]')
but each time got "NoSuchElementException: Unable to locate element: {"method":"xpath","selector": blablabla"
Please advice
Upvotes: 0
Views: 1065
Reputation: 3
actually what I've found is that the only thing I need is using implicitly_wait. this code works fine:
sign_in.click()
driver.implicitly_wait(30)
alert_text = driver.find_element_by_css_selector("div#alert_signin > div.alert.alert-danger.alert-dismissable").text
alert_text = alert_text.replace(u"×", "").strip()
assert alert_text == "Invalid username or password."
however note from alecxe was very useful while I was trying to verify my approach
You cannot directly point your expressions to the text nodes in Selenium.
Upvotes: 0
Reputation: 1736
Can you provide some more info, please:
-- Is there a button that you click to get the message?
-- Is the message within a browser-specific alert, or is it a displayed text on the page?
(if the latter is the case, try something like if "Invalid username or password." in driver.page_source: print "SUCCESS!")
Upvotes: 0
Reputation: 473813
You cannot directly point your expressions to the text nodes in Selenium.
Instead, I would get the entire "alert" text:
alert_text = driver.find_element_by_css_selector("#alert_signin .alert").text
Then, you can either apply the "contains" check:
assert "Invalid username or password." in alert_text
Or, remove the "x" part:
alert_text = alert_text.replace(u"×", "").strip()
assert alert_text == "Invalid username or password."
Examples in Python.
Upvotes: 1