Riaz Ladhani Riaz Ladhani - 1 year ago 268
Python Question

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

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

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
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.
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

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.

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)

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

Answer Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download