Java webdriver: Element not visible exception

前端 未结 5 823
一向
一向 2020-12-11 05:31

I\'m having the following problem. I have a dropdown that is hidden so when I make the Select and run the test i get the following error:

 org.openqa.seleniu         


        
相关标签:
5条回答
  • 2020-12-11 06:22

    Since WebDriver tries to simulate real users, it cannot interact with elements which are invisible/hidden. To solve your issue, I think you would need to click on div first which will make the drop down visible and select option from the dropdown. I would recommend such an approach as opposed to pure Javascript way since it would simulate a real user. Give following a shot,

    WebDriverWait wait = new WebDriverWait(driver, 300);
    WebElement triggerDropDown = driver.findElement(By
                    .className("ui-helper-hidden"));
    triggerDropDown.click();
    WebElement selectElement = wait.until(ExpectedConditions
                      .visibilityOfElementLocated(By.id("formLevel:levels_input")));
    Select select = new Select(selectElement);
    select.selectByVisibleText("SECURITY");
    

    Edit updated the class name of triggerDropDown

    0 讨论(0)
  • 2020-12-11 06:28

    Hi There can be so many reason for this. I also faced this issue number of times and solved using different ways.

    1- Using WebdriverWait that is also know as explicit wait

    2- Using unique xpath- using xpath ways.

    3- Get the size of element then click or perform any action on first one.

    I documented all solution here How to Solve Element not visible Exception

    0 讨论(0)
  • 2020-12-11 06:29

    In addition to the reasons and issues raised by the earlier answers, I encountered another cause worth mentioning. In my case, a JavaScript on the page had to run after clicking a link on the page in order for the elements that I wanted to access to become visible. That's ok as long as your driver has JavaScript enabled. In my case, I was running without JavaScript, so even though the link was "clicked" programmatically, the elements were not becoming visible. I was using HtmlUnitDriver with the default settings. Ultimately I switched to ChromeDriver. (You can enable JavaScript on HtmlUnitDriver, but that - for other reasons - was not enough for me in my case.)

    0 讨论(0)
  • 2020-12-11 06:31

    Haven't tested this, but does the following work?

    s.selectByValue("7ea4b4ea-4f06-4d27-9541-1b0cf3f2aa22");

    0 讨论(0)
  • 2020-12-11 06:32

    I absolutely agree to sircapsalot. You should hold application business logic and "do like an user". And use this hack for workarounds only.

    Answer:

    Try this way

    document.getElementById('formLevel:levels_input').options[3].selected = "true"

    0 讨论(0)
提交回复
热议问题