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

时间:2016-10-25 11:19:39

标签: python selenium selenium-webdriver

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

错误追踪:

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

我的代码段是:

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

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

我也尝试过:

@media screen and (max-width: 768px) {
  .tabs {
    overflow-x: scroll;
    white-space: nowrap;
  }
}

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

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

2 个答案:

答案 0 :(得分:7)

您用于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_)
  

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

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

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

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

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

答案 1 :(得分:3)

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

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