Codeception 无法点击它之前看到的元素 [英] Codeception can't click on element it has seen just before

查看:20
本文介绍了Codeception 无法点击它之前看到的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的 Stackoverflow 的人们,

Hello dear people of Stackoverflow,

目前我正在使用 Selenium 作为 WebDriver 模块使用 Codeception 编写验收测试.在我检查我们的子导航是否存在、是否完整且有效的测试中,我努力使用以下代码:

currently I am writing acceptance tests with Codeception using Selenium as WebDriver module. In my test to check if our sub-navigation exists, is complete and works I struggle with the following piece of code:

$I->see('Partners');
$I->click('Partners');

所有 see 调用都运行良好,但 click 调用失败并出现 ElementNotVisibleException 说明要单击的元素不可见,我不明白,因为 see 调用起作用了.

all the see calls are working perfectly, but the click call fails with an ElementNotVisibleException stating that the element to be clicked is not visible, which I don't understand since the see call worked.

为了确保我们没有在页面上看到任何旧的合作伙伴"字符串,但它确实是我想要点击的链接,我通过添加 a 选择器更改了调用.

To make sure we're not seeing any old "Partners" string on the page but it really is the link I want to click, I changed the call by adding the a selector.

$I->see('Partners', 'a');
$I->click('Partners');

尽管如此,我还是收到了之前的 ElementNotVisibleException.在下一步中,我向 click 调用添加了一个上下文:

Still, I'm getting the ElementNotVisibleException from before. In the next step, I added a context to the click call to this:

$I->click('Partners', 'a');

这使得异常消失了.尽管如此,点击永远不会发生,测试只是失败并显示以下消息:

Which made the exception disappear. Still, the click never happens, the test simply fails with the message:

未找到带有合作伙伴"的链接或按钮或 CSS 或 XPath 元素.

我什至在点击调用中使用了 XPath,如下所示://a[text()='Partners'] 并得到相同的消息.

I even used XPath in the click call like this: //a[text()='Partners'] and get the same message.

最让我烦恼的是,see 调用似乎看到了我试图点击的链接,而且,即使是失败后 Codeception 提供的页面源和屏幕截图也包含这一点链接,可见且在有效的 HTML 中.

What bothers me the most with this is that the see call seems to see the link I am trying to click and moreover, even the page source and screenshot provided by Codeception after a fail contain this very link, visible and in valid HTML.

我尝试点击子导航中的一些其他元素,结果相同,而更奇怪的是,点击主导航中的链接似乎工作正常.

I tried to click on some other elements within the sub-navigation with the same result, while - which is even more strange - clicks on links in the main navigation seem to work just fine.

我不知道为什么这不起作用,但非常感谢任何帮助.

I have no idea why this doesn't work but any help is greatly appreciated.

推荐答案

我不明白为什么在某些情况下会发生这种情况,而在其他情况下不会,但我找到了一个适合我的解决方案:使用 CSS仅限选择器.

I don't understand exactly why it happens in some cases and not others, but I've found a solution that works for me: Use CSS selectors only.

基本上在某些情况下(我认为这与单击 div/span 而不是单击按钮有关)->see() 将使用模糊文本字符串作为第一个参数,但是 ->click() 需要显式的 CSS 选择器.

Basically in some cases (I think it has to do with clicking on div/span rather than a/button) ->see() will work with the fuzzy text string as first argument, but ->click() requires the explicit CSS selector.

所以这将有我可以看到但不能点击"问题:

So this will have the "I can see but not click" problems:

// BROKEN
$I->see("All topics", ".header-taxonomy .taxonomy-list-title");
$I->click("All topics", ".header-taxonomy .taxonomy-list-title");

但这会起作用:

// WORKS
$I->see("All topics", ".header-taxonomy .taxonomy-list-title");
$I->click(".header-taxonomy .taxonomy-list-title");

显然它根本不理想,部分原因是它需要一个唯一的选择器,但对我来说主要是因为它使测试和测试输出的一致性和阅读难度降低.

Obviously it's not ideal at all, partly because it requires a unique selector, but for me mostly just because it makes the tests and testing output less consistent and harder to read.

无论哪种方式,我只是想让这个测试脚本完成,现在它可以工作了,因为我仍在使用 ->see() 和模糊文本定位器,​​我仍然确信在我点击之前文本已显示.

Either way, I just wanted to get this testing script finished, and now it's working, and because I'm still using ->see() with the fuzzy text locator, I'm still confident that the text is showing before I click.

值得注意的是,这个问题在使用 PHPBrowser 模块时不会发生,这让我很困惑.我升级"了到 WebDriver 并发现由于 ->click() 的不同行为,我的脚本在所有这些地方都被破坏了(除了预期需要在使用前打开菜单它们,在 PHPBrowser 中不需要)

Notably, this issue doesn't happen when using the PHPBrowser module, which is what tripped me up. I "upgraded" to WebDriver and found my scripts were broken in all these places due to the different behavior of ->click() (in addition to the expected need to open menus before using them, which wasn't needed in PHPBrowser)

这篇关于Codeception 无法点击它之前看到的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆