Selenium Python 等待文本出现在元素错误显示需要 3 个参数 2 给出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40238574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:19:53  来源:igfitidea点击:

Selenium Python wait for text to be present in element error shows takes 3 arguments 2 given

pythonseleniumselenium-webdriver

提问by Riaz Ladhani

I am using WebdriverWait to wait for some text to be present in an element on a webpage. I am using Selenium with Python. My syntax is not correct. I am getting the error: TypeError: init() takes exactly 3 arguments (2 given):

我正在使用 WebdriverWait 等待一些文本出现在网页上的元素中。我在 Python 中使用 Selenium。我的语法不正确。我收到错误:TypeError: init() 需要 3 个参数(给出 2 个):

Error trace:

错误跟踪:

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)

My code snippet is:

我的代码片段是:

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")

The scenario is the user clicks the run button and it starts of a process. When the process completes the text "No data to display" will be shown. I would like to wait until this text is displayed then I know the process has completed. Before I was using time.sleep(900) which is not good as it explicitly waits the full 15 mins. The process could complete in 8 mins, sometimes 12 mins.

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

I have also tried:

我也试过:

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"))

The error shows: TypeError: init() takes exactly 3 arguments (4 given)

错误显示:TypeError: init() 需要 3 个参数(给出 4 个)

What is the correct syntax to wait for the text to be present? Thanks, Riaz

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

回答by Naveen Kumar R B

The syntax you used for EC.text_to_be_present_in_element("No data to display"))is wrong.

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

The syntax is:

语法是:

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

检查给定文本是否存在于指定元素中的期望值。定位器,文本

So, clearly, the locator is missing in your code in which the text to be checked. putting parenthesis also the issue you are facing (in the second edit).

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

Use as follows (to add locator with correct parenthesis ):

使用如下(添加带有正确括号的定位器):

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

Note: By.id is just an example. you can use any locator to identify the element supported by selenium

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

回答by Andersson

Selector should be passed as tuple, but not as two separate arguments:

Selector 应该作为tuple传递,而不是作为两个单独的参数传递:

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

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

So try to replace

所以尝试更换

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"))

with

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"))