使用Selenium,是否有另一种更可靠的方法在Headless Chrome的元素上使用click命令? [英] Using Selenium, is there another, more reliable, way to use click command on an element in Headless Chrome?

查看:253
本文介绍了使用Selenium,是否有另一种更可靠的方法在Headless Chrome的元素上使用click命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过两天的努力,Headless Chrome遇到了不稳定的click()命令问题-在我的情况下是带有href标签的Anchor(a)元素-并经过了各种线程有关chromeOptions的建议(--start-maximized,- -window-size等),并尝试12种不同的方法(使用sendKeys和Actions甚至commit())来解决click()失败,但是没有成功...

After 2 days of struggling with an erratic click() command issue in Headless Chrome - in my case an Anchor (a) element with a href tag - and going through various threads advice about chromeOptions (--start-maximized, --window-size, etc...), and trying 12 different ways ( with sendKeys and Actions, and even submit() ) to work around the click() with no success...

使用ChromeDriver 77.0,Chrome 77.0.3865.75和Selenium 3.141.59,由于此click()不稳定,我的测试在Chrome中很稳定,而在Headless Chrome中却很不稳定:

Using ChromeDriver 77.0, Chrome 77.0.3865.75 and Selenium 3.141.59, my tests are stable in Chrome and they're unstable in Headless Chrome because of this erratic click():

例如::在Chrome和无头Chrome中都单击一个元素(在我的情况下是带有href标签的锚点(a)元素),然后检查是否出现另一个元素

E.g.: Click on an element (in my case an anchor (a) element with a href tag), in both Chrome and Headless Chrome, and check another element appears thereafter

添加循环并尝试在下面进行捕获可以稳定我的测试并使结果可靠!您还能想到另一种方法吗?

Adding the loop and try catch below stabilizes my tests and makes their results reliable! Is there another way you can think of?

Test() {
    for(int t = 0; t <= 2; t++) { //TRY CLICKING ON THE ELEMENT 3 TIMES
        WebElement element = 
    wait.until(ExpectedConditions.presenceOfElementLocated(elementToFind));
        wait.until(ExpectedConditions.visibilityOf(element));
        wait.until(ExpectedConditions.elementToBeClickable(element));

        try {
            element.click(); //ERRATIC CLICK() ON HEADLESS CHROME

 if(wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElementAfterClick)).isDisplayed() == true)
                break; //BUTTON WAS REALLY CLICKED
          } catch (TimeoutException toe) { //BUTTON WASN'T REALLY CLICKED
                if (t == 2) toe.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
     }
}

我说无头Chrome中的click()不稳定"是因为click()命令始终成功执行(否则,在单击该元素时会发现NoSuchElement或StaleElementReferenceException或任何其他异常),但是,有时,则该元素未被实际点击. 其他时间,相同的测试和代码可以流畅运行,并且元素实际上已被点击-我知道这是因为具有visibleOfElementLocated(expectedElementAfterClick)的行按预期执行.反过来,这个click()问题使我的测试不稳定. 因此,结果不可靠.

I'm saying "the click() is erratic in Headless Chrome" because the click() command always executes successfully (otherwise a NoSuchElement or a StaleElementReferenceException or any other exception would be found when clicking on the element) BUT, sometimes, the element is not actually clicked. Other times, the very same test and code runs smoothly and the element is actually clicked - I know this because the line with visibilityOfElementLocated(expectedElementAfterClick) executes as expected. This click() issue, in turn, makes my tests unstable. Thus, the results unreliable.

我怀疑这是硒的实际问题.

I suspect this to be an actual Selenium issue.

推荐答案

如果您的用例是要在某个 WebElement 上调用click(),请使其简短明了. presenceOfElementLocated()visibilityOf()将无济于事,您需要为elementToBeClickable()引入 WebDriverWait ,然后可以使用以下解决方案:

To keep it short and simple if your usecase is to invoke click() on a certain WebElement presenceOfElementLocated() and visibilityOf() won't help and you need to induce WebDriverWait for the elementToBeClickable() and you can use the following solution:

try {
  new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element).click();
  System.out.println("Element was clicked")
}
catch(TimeoutException e) {
  System.out.println("Element wasn't clicked")
}

您可以在 Selenium:Check for元素的存在

其他注意事项

确保:

  • The Locator Strategies you have used, uniquely idendentifies the element within the DOM Tree.
  • JDK is upgraded to current levels JDK 8u222.
  • Selenium is upgraded to current levels Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v77.0 level.
  • Chrome is updated to current Chrome Version 77.0 level. (as per ChromeDriver v77.0 release notes)

这篇关于使用Selenium,是否有另一种更可靠的方法在Headless Chrome的元素上使用click命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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