Selenium Python等待文本出现在元素error shows中,接受3个给定的参数2

2024-04-25 23:12:03 发布

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

我正在使用WebdriverWait等待网页元素中出现文本。我在Python中使用Selenium。我的语法不正确。 我得到了错误: TypeError:init()只接受3个参数(给定2个):

错误跟踪:

Traceback (most recent call last):
  File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Regression_TestCase\RegressionProjectEdit_TestCase.py", line 2714, in test_000057_run_clean_and_match_process
    process_lists_page.wait_for_run_process_to_finish()
  File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Pages\operations.py", line 334, in wait_for_run_process_to_finish
    EC.text_to_be_present_in_element("No data to display"))
TypeError: __init__() takes exactly 3 arguments (2 given)

我的代码片段是:

def wait_for_run_process_to_finish(self): # When the process is running use WebdriverWait to check until the process has finished.  No data to display is shown when process has completed.
    try:
        WebDriverWait(self.driver, 900).until(
            EC.text_to_be_present_in_element("No data to display"))
        no_data_to_display_element = self.get_element(By.ID, 'operations_monitoring_tab_current_ct_fields_no_data')
        print "no_data_to_display_element ="
        print no_data_to_display_element.text
        if no_data_to_display_element.text == "No data to display":
            return True
    except NoSuchElementException, e:
        print "Element not found "
        print e
        self.save_screenshot("wait_for_run_process_to_finish")

场景是用户单击run按钮,它启动一个进程。 当进程完成时,将显示文本“没有要显示的数据”。 我想等到显示此文本,然后我知道该过程已完成。 在我使用time.sleep(900)之前,它不是很好,因为它显式地等待了整整15分钟。这个过程可以在8分钟内完成,有时12分钟。

我也试过:

WebDriverWait(self.driver, 900).until(
            EC.text_to_be_present_in_element(By.ID, 'operations_monitoring_tab_current_ct_fields_no_data', "No data to display"))

错误显示:TypeError:init()只接受3个参数(给定4个)

等待文本出现的正确语法是什么? 谢谢,里亚兹


Tags: tonoruntextintest文本self
2条回答

选择器应作为元组传递,但不能作为两个单独的参数传递:

(By.TYPE, VALUE, TEXT)-->;((By.TYPE, VALUE), TEXT)

所以试着替换

WebDriverWait(self.driver, 900).until(
        EC.text_to_be_present_in_element(By.ID, 'operations_monitoring_tab_current_ct_fields_no_data', "No data to display"))

WebDriverWait(self.driver, 900).until(
        EC.text_to_be_present_in_element((By.ID, 'operations_monitoring_tab_current_ct_fields_no_data'), "No data to display"))

用于EC.text_to_be_present_in_element("No data to display"))的语法错误。

语法为:

class selenium.webdriver.support.expected_conditions.text_to_be_present_in_element(locator, text_)

An expectation for checking if the given text is present in the specified element. locator, text

所以,很明显,你要检查的文本代码中缺少定位器。插入括号也是您面临的问题(在第二次编辑中)。

如下所示(添加带正确括号的定位器):

EC.text_to_be_present_in_element((By.ID, "operations_monitoring_tab_current_ct_fields_no_data"), "No data to display")

注:By.id只是一个例子。您可以使用任何定位器来标识selenium支持的元素

相关问题 更多 >