等待send_keys完成后再单击按钮(使用Selenium)



我正在学习Selenium并尝试自动化一项任务。该任务包括从典型的输入文件上传一个zip文件,然后单击上传按钮。我已经让它工作了,但有时它会失败,因为在send_keys完成之前按下了上传按钮,页面显示一个对话框,说没有什么可上传的。

这是代码:

driver = webdriver.Firefox()
driver.get('http://localhost:8008/management/#/welcome')
WebDriverWait(driver, 15).until(
EC.url_matches('http://localhost:8008/management/#/welcome'))
driver.get('http://localhost:8008/management/#/configuration-engines')
WebDriverWait(driver, 15).until(EC.url_matches('http://localhost:8008/management/#/configuration-engines'))
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.ID, 'userfile'))).send_keys(file_path)
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.ID, 'upload_and_install')))
button = driver.find_element_by_id('upload_and_install').click()

这是输入的HTML代码(无论有没有文件,它都不会改变。他们没有值来检查它是否已经改变(:

<input type="file" name="file[]" id="userfile" dir="ltr">

我找到的解决方案是简单地做一个时间。睡眠(5(,但我想知道是否有更多的"睡眠";"干净";方法

正如OP所说,有一个轮子在旋转,我建议您使用ExplicitWait - invisibility_of_element

示例代码:

WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "Wheel spining xpath"))) 

这一行将等待所提供的xpath不可见。

最新更新