在只读框中查找单词selenium和python

2024-03-28 22:01:21 发布

您现在位置:Python中文网/ 问答频道 /正文

<textarea id="value(Orders21)" class="readonly" style="width: 98%;" readonly="readonly" rows="10" cols="20" name="value(Orders21)">HOMEBOUND REASONS: NEEDS ASSISTANCE</textarea>

嘿,伙计们,这就是firebug在我工作的页面上显示的内容。在

我想知道如何设置一个条件来告诉我这个只读文本框包含单词“homeboundreasons:”

^{pr2}$

这将产生此错误(TypeError:'str'对象不可调用)

我该怎么办?在


Tags: nameidvaluestylewidthclassrowscols
3条回答

您可以找到具有xpath表达式的元素列表,然后测试find_elements_by_返回的列表的大小:

elements = browser.find_elements_by_xpath("//textarea[@class='readonly' and contains(text(), 'HOMEBOUND REASONS')]")
if len(elements) > 0:
    print("it's there")
else:
    print("nope not here")
element = browser.find_element_by_id('value(Orders21)')

if "HOMEBOUND REASONS:" in element.text:
    print("it's there")
else:
    ("nope not here")

以下是对上述代码的解释:

这是在页面中定位元素

^{pr2}$

接下来是if条件来验证是否找到了特定元素

 if "HOMEBOUND REASONS:" in element.text:
        print("it's there")
    else:
        ("nope not here")

这里element.text是获取元素/对象中的文本,然后我们比较特定的字符串是否存在于元素文本中

•首先找到元素

•查找文本

•进行逻辑比较。在

element = browser.find_element_by_id('value(Orders21)')

if "HOMEBOUND REASONS:" in element.text:
    print("it's there")
else:
    ("nope not here")

相关问题 更多 >