等待Selenium Webdriver中的按钮状态更改



我正在测试一个文本框和按钮-用户在文本框中输入一些信息,然后点击按钮-按钮将文本从"Update"更改为"Updating",然后在操作完成后返回到"Update"。

我想让Selenium等待,直到按钮返回到"更新"之前继续。

我已经尝试了使用xpath的各种解决方案,这里是我现在的位置:

self.wait.until(EC.element_to_be_clickable(By.XPATH, '//button[@id="send_data"][text()="Update"]'))

按钮的HTML格式如下:

<button id="send_data" type="button" class="btn btn-success" onclick="ChangeParam(this,'radio','freq'; return false;">Update</button>

<button id="send_data" type="button" class="btn btn-default" onclick="ChangeParam(this,'radio','freq'; return false;">Updating</button>

我哪里错了?

这是一个很常见的问题:你应该发送type_of_locator, "locator_string"作为参数((By.XPATH, "//button"))的元组,而不是作为两个单独的参数(By.XPATH, "//button")

使用以下代码

self.wait.until(EC.element_to_be_clickable((By.XPATH,'//button[@id="send_data"][text()="Update"]')))

另一个应该工作的解决方案是使用EC.invisibility_of_element_located。点击更新按钮后,可以等待"更新"状态按钮所代表的元素不可见。

self.wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'button#send_data .btn-default')))

最新更新