python - 单击 Selenium 中的链接后让 Firefox 等待新页面加载?

标签 python selenium selenium-chromedriver selenium-firefoxdriver

我刚刚开始使用 Selenium,并使用 Chrome 编写了一堆测试。然后我尝试使用 Firefox 运行相同的测试,但大多数都失败了。

我有一堆有点像这样的测试:

1. Find link
2. Click link
3. Confirm the title of the new page matches what I expect

这在 Chrome 中工作正常,但在 Firefox 中,第 3 步似乎在浏览器有时间加载页面之前立即执行。如果我添加几秒钟的等待,所有测试都会通过,但我宁愿避免这种情况。

这是一个配置问题,还是有更好的方法来编写测试来帮助提高兼容性?

这些是在 Chrome 中有效但在 Firefox 中失败的测试的基础知识

link = driver.find_element_by_link_text(link_text)
link.click()
# Check if `driver.title` contains `title`
assert title in driver.title

在点击后插入 time.sleep(2) 确实可以使其工作。

(我在身份验证测试中遇到了同样的错误:填写表单,单击提交,确认用户被转发到正确的页面。在 Chrome 中,这通过了,在 Firefox 中,对登录页面进行了前向检查,因为浏览器仍然没有完成重定向到新页面。我收到测试失败的报告,一秒钟后浏览器呈现预期的页面。)

最佳答案

您可以申请以下内容:

from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

current_title = driver.title # get current title
link = driver.find_element_by_link_text(link_text)
link.click()
wait(driver, 10).until(lambda driver: driver.title != current_title) # wait for title to be different than current one
assert title in driver.title

关于python - 单击 Selenium 中的链接后让 Firefox 等待新页面加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42362335/

相关文章:

node.js - 如何将组合键发送到 selenium chromedriver?

python - 在 Python 的类名中使用空格

python - 抓取 Amazon 作业结果时不断收到连接重置错误 10054

Python/Selenium - Chrome Web 驱动程序,单击操作

Python - 获取 HTML 标签之间的文本

python - 为什么我的 Selenium 不停止加载?

python - 如何让 Sprite 旋转面向鼠标?

python - 为什么中断比引发异常更快?

javascript - 如何使用 python 编码读取特定网站的控制台日志(例如 ok、connected.. 等消息)?

c# - Selenium ChromeDriver C# - 如何发送快捷浏览器