我使用try-catch块中的Contains text通过xpath查找元素的方法是返回fals

2024-09-28 17:01:19 发布

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

我在Python中使用Webdriver 我有一个方法可以检查网页上的项目名称是否包含预期值。
我的方法是返回false。我原以为它会变成真的。在

我使用下面的xpath检查元素是否包含项目名称

self.driver.find_element_by_xpath ('//*[contains(text(), %s)]' % project_name)

例如,我要查找的项目名称值是LADEMO_IE_05_21_1510_18_31

当我打印xpath时,控制台返回以下输出 在那里(第五排)。在

^{pr2}$

输出如下:

JMeter_Reports_MegaOneCC

LAdemo

Sales Demo

Normal

LADEMO_IE_05_21_1510_18_31

LADEMO create a basic project test script - Selenium Webdriver/Python Automated test

LADEMO_IE_05_21_1510_20_44

我的代码片段在方法和调用该方法的测试用例的下面。在

从页面\管理员py在

import datetime
from selenium.common.exceptions import NoSuchElementException
from Locators.locators import MainPageLocators
from Locators import locators
from Locators import element
from Locators.element import BasePageElement
from Pages.base import BasePage
#from Pages.administrationPage import AdministrationPage
import re

class AdministrationPage(BasePage):

def is_project_text_present2(self, project_name):
        try:
            project_name_element = self.driver.find_element_by_xpath ('//*[contains(text(), %s)]' % project_name)
            print project_name_element.text
        except NoSuchElementException, e:
            return False
        return project_name_element.text in project_name

测试用例\管理页面_测试用例.py在

import unittest
import time
import datetime
from selenium import webdriver
from Locators import locators
from Locators import element
from Pages import login
from Pages import admin
import datetime

class AdministrationPage_TestCase(unittest.TestCase):

def test_add_Project(self):
    print "*** test_add_project ***"
    #this var contains the project name value e.g. LADEMO_IE_05_20_152015 which is then used for project_name_textfield.send_keys(project_name_fieldValue) from the add_project method
    project_name = "LADEMO_IE_" + self.get_datetime_now()
    print "project_name_value" + project_name
    login_page = login.LoginPage(self.driver)
    login_page.userLogin_valid("riaz.ladhani", "pass123")
    administration_page = login_page.clickAdministration()
    administration_page.add_project(project_name)
    try:
        administration_page.is_project_text_present2(project_name)
        print administration_page.is_project_text_present2(project_name) # false is printed here on the console. I want this to be true because the project name text does exist
    except AssertionError as e:
        self.verificationErrors.append(str(e))
    print "I am here in except AssertionError as e: administration_page.is_project_text_present2(project_name)"

控制台的输出是

假 我在这里除了断言错误外: 管理_第2页“项目”文本“当前”2(项目名称)

我的返回false和not true的方法是否有错误?在

HTML是:

    <table cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
    <tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="0">
    <tr class="GAT4PNUEH" __gwt_subrow="0" __gwt_row="1">
    <tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="2">
    <tr class="GAT4PNUEH" __gwt_subrow="0" __gwt_row="3">
    <tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="4">
    <tr class="GAT4PNUEH" __gwt_subrow="0" __gwt_row="5">
    <tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="6">
    <tr class="GAT4PNUEH GAT4PNUMG" __gwt_subrow="0" __gwt_row="7">
    <td class="GAT4PNUEG GAT4PNUFH GAT4PNUHG GAT4PNUNG">
    <td class="GAT4PNUEG GAT4PNUFH GAT4PNUNG">
        <div __gwt_cell="cell-gwt-uid-113" style="outline-style:none;">
            <span class="linkhover" title="LADEMO_IE_05_21_1510_18_31" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">LADEMO_IE_05_21_1510_18_31</span>
        </div>
    </td>
    <td class="GAT4PNUEG GAT4PNUFH GAT4PNUNG">
    <td class="GAT4PNUEG GAT4PNUFH GAT4PNUNG">
    <td class="GAT4PNUEG GAT4PNUFH GAT4PNUBH GAT4PNUNG">
</tr>
<tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="8">
    <td class="GAT4PNUEG GAT4PNUGG GAT4PNUHG">
    <td class="GAT4PNUEG GAT4PNUGG">
        <div __gwt_cell="cell-gwt-uid-113" style="outline-style:none;">
            <span class="linkhover" title="LADEMO_IE_05_21_1510_20_44" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">LADEMO_IE_05_21_1510_20_44</span>
        </div>
    </td>
        <td class="GAT4PNUEG GAT4PNUGG">
       <td class="GAT4PNUEG GAT4PNUGG">
       <td class="GAT4PNUEG GAT4PNUGG GAT4PNUBH">
     </tr>
     <tr class="GAT4PNUEH" __gwt_subrow="0" __gwt_row="9">
     <tr class="GAT4PNUFG" __gwt_subrow="0" __gwt_row="10">
     ...
  </tbody>
</table>

Tags: textnamefromimportprojectelementtrie
2条回答

问题可能出在你的时间延迟上。我想当您调用administration_page.add_project(project_name)时,添加项目可能需要一些时间,下一行administration_page.is_project_text_present2(project_name)将返回false,因为页面还没有准备好。当你在控制台中尝试时,你总能找到它。 为了验证这个想法,请添加一些waiting beforeis_project_text_present2方法。如果可以的话,我建议使用waiting to the specific element方法。在

我现在已经设法解决了。我更改了XPath以在表Span类单元中查找项目名称。 现在,当找到项目名称LADEMO_IE_05_21_1510_18_31时,它将返回true

下面是我的代码片段:

页数\管理员py在

    def is_project_name_present(self, project_name):
        try:
            test = self.driver.find_element_by_xpath('//span[contains(@class, "linkhover")][text()= "%s"]' % project_name)
        except NoSuchElementException, e:
            return False
        return project_name in test.text # Check if the project name is in the list of projects (list of projects is in the html table, /td class span

测试用例\管理页面_测试用例.py在

^{pr2}$

相关问题 更多 >