Html 量角器:按 Div 文本查找元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32016080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protractor: Finding Element by Div Text
提问by CapturedTree
Hey I have this code in one of my div elements:
嘿,我的 div 元素之一中有此代码:
<div class="col-sm-8">Account Information: </div>
Can someone tell me how I would go about finding this element in my protractor code? Is it possible to do something like this:
有人能告诉我如何在我的量角器代码中找到这个元素吗?是否有可能做这样的事情:
expect(element(by.divText('Account Information: ')).isDisplayed()).toBe(true);
expect(element(by.divText('Account Information: ')).isDisplayed()).toBe(true);
I have multiple elements with the class "col-sm-8" so I am not able to find the element by class. I was just wondering if there is any way to possibly find the element using the text in the div element? Thanks for the help!
我有多个类为“col-sm-8”的元素,所以我无法按类找到该元素。我只是想知道是否有任何方法可以使用 div 元素中的文本找到该元素?谢谢您的帮助!
回答by Andres D
I would recommend you to use by.cssContainingText
我建议你使用by.cssContainingText
element(by.cssContainingText('.col-sm-8', 'Account Information'))
回答by finspin
There is no webdriver method which would allow locating an element by its text. You could try using xpath in the following way (not tested):
没有允许通过文本定位元素的 webdriver 方法。您可以尝试通过以下方式使用 xpath(未测试):
element(by.xpath('//div[contains(text(), "Account Information: ")]')