我有一些带有一些列的html表。每行显示报告的名称 我可以选择一个报告,然后单击“运行”按钮来处理报告。 文字"查看"单击“报告”按钮后,它将显示在“视图”列中。 "视图"文字不会出现在它自己的上面。 在文本出现之前可以点击几下。 我想继续点击“报告”按钮,直到"查看"文字出现
我认为最好的解决方案是以某种方式将其置于while循环中。
我试过,但是我收到了错误:
UnboundLocalError: local variable 'element' referenced before assignment
错误跟踪是:
Error
Traceback (most recent call last):
File "C:\Webdriver\ClearCore\TestCases\ReportingPage_TestCase.py", line 81, in test_1_add_matches_report
print reports_page.is_view_link_present_to_view_the_report("Matches")
File "C:\Webdriver\ClearCore\Pages\reports.py", line 321, in is_view_link_present_to_view_the_report
view_element = self.get_element(By.XPATH, '//table[@id="reporting_reports_ct_fields_body"]//td[.//span[text()="%s"]]/following-sibling::td[2]//span[text()="view"]' % report_name)
File "C:\Webdriver\ClearCore\Pages\base.py", line 34, in get_element
return element
UnboundLocalError: local variable 'element' referenced before assignment
使用While循环等待元素找到的方法是:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
class ReportsPage(BasePage):
def __init__(self, d):
super(ReportsPage, self).__init__(d)
self.datamap_name = UsefulHelperMethods.read_from_file("datamap_name")
self.tool_bar = ToolbarPage(self.driver)
def is_view_link_present_to_view_the_report(self, report_name):
# Params : report_name : Name of the report which to select to view
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'reporting_reports_ct_fields_body')))
found = False
while not found:
try:
view_element = self.driver.find_element(By.XPATH, '//table[@id="reporting_reports_ct_fields_body"]//td[.//span[text()="%s"]]/following-sibling::td[2]//span[text()="view"]' % report_name)
found = True
except NoSuchElementException:
time.sleep(2)
self.tool_bar.click_reports_button_from_toolbar()
在我的基类中定义的元素是:
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
class BasePage(object):
def __init__(self, driver):
self.driver = driver
self.driver.implicitly_wait(120)
# 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
return element
我调用该方法的TestCase类是:
from Menus.project_navigator import ProjectNavigatorPage
from Menus.toolbar import ToolbarPage
from Pages.reports import ReportsPage
class ReportingPage_TestCase(BaseTestCase):
def test_1_add_matches_report(self):
tool_bar = ToolBarPage(self.driver)
project_navigator = ProjectNavigatorPage(self.driver)
reports_page = project_navigator.select_projectNavigator_item("Reports")
reports_page.click_add_button_for_reports()
reports_page.select_a_report_checkbox_from_reports_page("Matches")
reports_page.click_run_button_to_run_the_report()
reports_page.click_ok_from_reports_dialog()
tool_bar.click_reports_button_from_toolbar()
print reports_page.is_view_link_present_to_view_the_report("Matches")
如何继续点击报告按钮(比如每2秒)并检查"视图"文字存在。继续尝试约5分钟。 如果在5分钟结束时它仍然没有找到视图链接然后打印未找到的视图文本,测试失败? 我不想使用time.sleep,因为这不是很有效。视图文本可能会在几秒或一分钟内出现,具体取决于报告的大小。
谢谢,帮助表示感谢。 里亚兹
HTML是:
<table id="reporting_reports_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GLKP2TGBFG GLKP2TGBMG" __gwt_subrow="0" __gwt_row="0">
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBHG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7232" style="outline-style:none;" tabindex="0">
<input type="checkbox" tabindex="-1" />
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7233" style="outline-style:none;">
<span class="linkhover" title="Matches" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Matches</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7234" style="outline-style:none;">
<span class="" title="manual" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">manual</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7235" style="outline-style:none;">
<span class="linkhover block" title="view" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">view</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7236" style="outline-style:none;">
<span class="" title="Matches report" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Matches report</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7237" style="outline-style:none;">
<span class="" title="USN entities" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">USN entities</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7238" style="outline-style:none;">
<span class="" title="05/11/2015 15:24:47" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">05/11/2015 15:24:47</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7239" style="outline-style:none;">
<span class="block" title="" style="">12</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBBH GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7240" style="outline-style:none;">
<span class="" title="INFOSHARE\riaz.ladhani" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">COMPANY\riaz.ladhani</span>
</div>
</td>
</tr>
<tr class="GLKP2TGBEH" __gwt_subrow="0" __gwt_row="1">
</tbody>
答案 0 :(得分:0)
元素仅存在于try块中,您必须在try块之外定义它。在伪代码中,它看起来像:
try
{
var element;
}
...
return element; // but does not exist in this scope here
只需在你的尝试中退回即可,你很好,不需要额外的一行:
try:
return self.driver.find_element(by=how, value=what)
except NoSuchElementException, e:
print what
print "Element not found "
print e
return None
要继续单击您可以使用WebDriverWait的按钮(如果在None
中找不到该元素,则需要返回get_element
。)
创建一个新方法,点击并检查元素
def clickTillView:
reportElement.click()
return get_element(self, how, what) != None
然后你可以等待clickTillView成为真正的元素存在
new WebDriverWait(driver, 10000, 1, None).until(clickTillView(), "view was not found in time")
原谅我的Python错误,我是一个Java家伙:p但这基本上就是你如何解决它。