我在try catch块中使用Contains文件通过xpath查找元素的方法返回false

时间:2015-05-21 12:38:59

标签: python selenium selenium-webdriver webdriver

我正在使用Webdriver和Python 我有一个方法,检查网页上的项目名称是否包含预期值 我的方法返回false。我期待它回归真实。

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

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

E.g。我正在寻找的项目名称值是LADEMO_IE_05_21_1510_18_31

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

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

print project_name_element.text

输出如下:

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

我的代码片段位于方法和调用该方法的测试用例之下。

来自Pages \ admin.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

的TestCases \ AdministrationPage_TestCase.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)"

控制台的输出是

假 我在这里,除了AssertionError为e: administration_page.is_project_text_present2(PROJECT_NAME)

我的方法中有什么不正确的地方返回false而不是真的吗?

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>

2 个答案:

答案 0 :(得分:1)

问题可能在于你有时间延迟。我想当你致电administration_page.add_project(project_name)时,可能需要一些时间来添加项目,而下一行administration_page.is_project_text_present2(project_name)将返回false,因为页面仍未准备就绪。当您在控制台中尝试它时,您总能找到它。 要验证此想法,请在is_project_text_present2方法之前添加一些等待。如果它能够工作,我建议使用等待特定元素的方法。

答案 1 :(得分:0)

我现在设法解决了这个问题。我更改了XPath以在表Span类单元格中找到projet名称。 它现在在找到项目名称LADEMO_IE_05_21_1510_18_31时返回true

这是我的代码段:

页面\ admin.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

的TestCases \ AdministrationPage_TestCase.py

administration_page.is_project_name_present(project_name)
    assert administration_page.is_project_name_present(project_name), "project name " + project_name + "not found"