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

My method to find element by xpath using Contains text in try catch block is returning false

我正在将 Webdriver 与 Python 一起使用 我有一个方法可以检查网页上的项目名称是否包含预期值。
我的方法是 returning false。我期待它 return 正确。

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

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

例如我正在寻找的项目名称值是 LADEMO_IE_05_21_1510_18_31

当我打印 xpath 时,以下输出在控制台中 returned & 它是 那里(向下第 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 as e: administration_page.is_project_text_present2(project_name)

我的方法是否有错误 return false 而不是 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>

问题可能出在您的时间延迟上。我想当您调用 administration_page.add_project(project_name) 时可能需要一些时间来添加项目并且您的下一行 administration_page.is_project_text_present2(project_name) 将 return false 因为页面还没有准备好。当您在控制台中尝试时,您总能找到它。 为了验证这个想法,请添加一些 waiting before is_project_text_present2 方法。如果可行,我建议使用等待特定元素的方法。

我现在已经设法解决了这个问题。我更改了 XPath 以在 table Span class 单元格中查找项目名称。 当它找到 LADEMO_IE_05_21_1510_18_31

的项目名称时,它现在 returns 为真

这是我的代码片段:

Pages\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"