这是源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>
我真正需要的是检查消息“无效的用户名或密码”。出现(python + selenium webdriver)。但是我很不幸使用xpath像这样找到它
find_element_by_xpath('//div[@id=\'alert_signin\']/div[@class=\'alert\']').text
因此,我决定使用消息文本来查找确切的xpath。我尝试了几种选择,例如
find_element_by_xpath('//*[text()[contains(.,\'Invalid\')]]')
或者
find_element_by_xpath('//*[contains(., \'Invalid username or password.\')]')
但每次都收到“ NoSuchElementException:无法找到元素:{“ method”:“ xpath”,“ selector”:blablabla“
请指教
您不能直接将表达式指向Selenium中的文本节点。
相反,我将获得整个“警报”文本:
alert_text = driver.find_element_by_css_selector("#alert_signin .alert").text
然后,您可以应用“包含”检查:
assert "Invalid username or password." in alert_text
或者,删除“ x”部分:
alert_text = alert_text.replace(u"×", "").strip()
assert alert_text == "Invalid username or password."
Python中的示例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句