[英]Get text from a label using Selenium (Python)
I'm trying to read the text from a label that changes according to certain conditions (eg. if you enter a Username already in use, the label will display "Username already in use"). 我正在尝试从根据某些条件而变化的标签中读取文本(例如,如果您输入已使用的用户名,则标签将显示“已使用的用户名”)。
I've been trying to read in the text from the label that gets displayed, but nothing I've tried has worked so far. 我一直试图从显示的标签中读取文本,但是到目前为止,我没有尝试过。
The HTML looks like this: HTML看起来像这样:
<div class="margin-top-10">
<span ng-show="sentValidation">
<span id="test1" ng-show="userNameAvailable" class="txt-color-green">
<i class="fa fa-check fa-lg"></i>Username available
</span>
<span id="test2" ng-show="!userNameAvailable && userNameAvailable != null" class="txt-color-red">
<i class="fa fa-exclamation-circle"></i>Username unavailable
</span>
</span>
<div class="txt-color-red" ng-show="form.cUsername.$dirty && form.cUsername.$invalid">
<p id="test3" ng-show="form.cUsername.$error.required">
<i class="fa fa-exclamation-circle"></i>Username is required
</p>
<p id="test4" class="txt-color-red" ng-show="form.cUsername.$error.maxlength">
<i class="fa fa-exclamation-circle"></i>Maxmium length is 50 characters
</p>
<p id="test5" class="txt-color-red" ng-show="form.cUsername.$error.minlength">
<i class="fa fa-exclamation-circle"></i>Minimum length is 4 characters
</p>
</div>
</div>
Does anyone know how I can read what gets displayed on the screen? 有谁知道我如何阅读屏幕上显示的内容?
content = driver.find_element_by_class_name('fa-exclamation-circle')
content.is_displayed()
You can use the find_element_by_class_name built in method. 您可以使用内置方法find_element_by_class_name。
You need to pass the class name as a parameter. 您需要将类名作为参数传递。
I didn't manage to find a way to read the text, but I found another solution. 我没有设法找到一种阅读文本的方法,但是我找到了另一种解决方案。 Instead of checking what text is displayed on the screen, I check to see which of the labels is being displayed (I'm able to do this because each of the labels is given a unique ID.
我没有查看屏幕上显示的文本,而是查看正在显示的标签(我能够执行此操作,因为每个标签都有唯一的ID。
So the code will look something like: 因此,代码如下所示:
Name=driver.find_element_by_id('test2')
if Name.is_displayed():
print ("Element found")
else:
print ("Element not found")
content = driver.find_element_by_class_name('fa-exclamation-circle')
print (content.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.