Selenium click not always working

独自空忆成欢 提交于 2019-11-30 16:55:41

问题


I have some tests which click on a tab, however the click is not always performed.

  • The xpath is correct as most of the times the test works

  • It is not a timing issue as I ve used thread.sleep() and other methods to ensure that the element is visible before clicking

  • The test believes that it is performing the click as it is not throwing an ElementNotFoundException or any other exceptions when 'performing' the click. The test fails later on after the click since the tab content would not have changed.

Further Info I am using Selenium 2.44.0 to implement tests in Java which run on Chrome 44.0.2403.107 m.

Is there something else that I can do or could this be an issue with selenium?


回答1:


There are several things you can try:

  • an Explicit elementToBeClickable Wait:

    WebDriverWait wait = new WebDriverWait(webDriver, 10);
    
    WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("myid")));
    button.click()
    
  • move to element before making a click:

    Actions actions = new Actions(driver);
    actions.moveToElement(button).click().build().perform();
    
  • make the click via javascript:

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].click();", button);
    



回答2:


you can go with linkText if the tab name contains any unique string. And make sure your tab is not dynamic. It should be visible in source code(manual source code(ctrl+u)).




回答3:


The following method work for me

WebElement button = SeleniumTools.findVisibleElement(By.cssSelector("#cssid"));

Actions actions = new Actions(driver);

actions.moveToElement(button).click().build().perform();


来源:https://stackoverflow.com/questions/31725033/selenium-click-not-always-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!