加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-2.7 – Selenium Python我从文本元素中获取一个空字符串,

发布时间:2020-12-20 13:10:03 所属栏目:Python 来源:网络整理
导读:我在GUI上显示了一些文本.我已经从Firebug的Xpath Checker中使用了它的XPATH来识别元素.该元素正确突出显示. 在我的代码中,我使用text属性来打印出它的值. 我想将值打印到控制台. 当我使用PyCharm调试器遍历代码时,我可以看到变量text属性为空. 为什么变量没
我在GUI上显示了一些文本.我已经从Firebug的Xpath Checker中使用了它的XPATH来识别元素.该元素正确突出显示.
在我的代码中,我使用text属性来打印出它的值.
我想将值打印到控制台.
当我使用PyCharm调试器遍历代码时,我可以看到变量text属性为空.
为什么变量没有得到文本值?

我的代码片段是:

def is_engine_number_displayed(self):
    engine_no_element = self.get_element(By.XPATH,'//a[contains(.,"Engine: 5.1.1")]')
    print "engine_no_element.text***"
    print engine_no_element.text
    #return engine_no_element.text in "Engine: 5.1.1"

HTML是:

<div class="GJPPK2LBKQ">
<a class="gwt-Anchor GJPPK2LBMQ" title="Click for about">Client: 5.1.1.6044</a>
<a class="gwt-Anchor GJPPK2LBMQ" title="Click for about">Engine: 5.1.1.5218</a>

我的XPATH找到文本引擎:5.1.1

//a[contains(.,"Engine: 5.1.1")]

使用XPATH,GUI上突出显示文本Engine:5.1.1

get_element在我的Base类中.它的实施是:

# returns the element if found
def get_element(self,how,what):
    # params how: By locator type
    # params what: locator value
    try:
        element = self.driver.find_element(by=how,value=what)
    except NoSuchElementException,e:
        print what
        print "Element not found "
        print e
        screenshot_name = how + what + get_datetime_now() # create screenshot name of the name of the element + locator + todays date time.  This way the screenshot name will be unique and be able to save
        self.save_screenshot(screenshot_name)
        raise
    return element

为什么我的元素文本值为空?

谢谢,
里亚兹

解决方法

当我从下拉列表中进行选择后尝试验证文本字段的数量时,我遇到了这个问题.正如你所提到的,文本确实在那里,但是selenium认为它们是空的.如果内存服务,那是因为表单还没有提交,我相信value属性(不是text属性)是getText()函数实际提取其数据的地方. value属性直到稍后才会设置.提交页面后,请查看元素的value属性,我相信这些值将被设置,getText()将返回正确的文本值.我无法找到解决办法,因为当用户点击提交按钮时,它正在进入下一个网页.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读