selenium webdriver: org.openqa.selenium.ElementNot

2020-02-12 05:38发布

问题:

I am trying to click on the span with the text- clone concept. Following is the html

<ul class="context-menu-list context-menu-root" style="width: 210px; top: 500px; left: 231px; z-index: 2;">
    <li class="context-menu-item">
    <li class="context-menu-item">
    <li class="context-menu-item disabled">
    <li class="context-menu-item">
    <li class="context-menu-item icon icon-evn-icon-clone-concept">
        <span>Clone concept</span>
    </li>
    <li class="context-menu-item">
    <li class="context-menu-item icon icon-delete disabled">
</ul>

the javascript code i use is:

driver.findElement(By.xpath("//span[text()='Clone concept']")).click();

I verified that this is the right for the element through firepath.

I also made sure that element is visible as per the link How to force Selenium WebDriver to click on element which is not currently visible?

Here is the computed css

font-family Verdana,?Arial,?Helvetica,?sans-serif
    .context-menu-list  Verdana,?Arial,?Helvetica,?sans-serif   
    jquery...enu.css (line 15)
    body    Arial,?Helvetica,?sans-serif    
    swa.css (line 3)
    font-size   11px
    .context-menu-list  11px    
    jquery...enu.css (line 15)
    list-style-type none
    .context-menu-list  none    
    jquery...enu.css (line 15)

Also tried the following code:

WebElement foo = driver.findElement(By.xpath("//span[text()='Clone concept']"));
Actions bar = new Actions(driver);
bar.click(foo).perform(); 

Exception: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.04 seconds Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 16:53:24' System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0' Driver info: driver.version: RemoteWebDriver

Any help will be appreciated.

Another hack for those who get stuck here:
For the time being I have been able to move forward by splitting this huge test case into simpler test cases.

回答1:

Unfortunately Webdriver doesn't seem to be great at handling situations like that described in your questions. You have a couple of options though. Mock a click using Javascript:

JavascriptLibrary jsLib = new JavascriptLibrary(); 
jsLib.callEmbeddedSelenium(selenium,"triggerMouseEventAt", elementToClick,"click", "0,0");

or

((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementToClick);

Or you can play around with using actions to click all of the elements in the menu chain. Unfortunately I have found this to be unreliable.

I have a script which detects whether an element is in a menu chain and if it is clicks on them in the required order to finally click on the one the user wanted if you want it I can post it somewhere but it isn't pretty or short.



回答2:

For the above query, here is the xpath:

//ui[@class='context-menu-list context-menu-root']/span[contains(text(),'Clone concept')]


回答3:

The problem is in your xpath. selenium webdriver is finding a duplicate element by your xpath in screen which is hidden and tring to perform operation on it. Please change the xpath and it will work. I did the same thing in my code..



回答4:

Basically, there are four reasons why an element is not interactable. No four is usually the solution.

1) Timing - the time it takes for elements to load. For this, you need to check how to use implicit an explicit wait

2) Check if the element is in a frame

3) Incorrect locator

4) Wrong implementation of responsiveness. This still stems from no 3). Some websites have only one code turned on for mobile and web versions. So, the element will have more than one instance when you check the xxxxx.size. You will have to search through the list for the one whose display != none. Then, you can append the position of the element to your xpath or whatever locator you are using. E.g. xxxx/yyyyy/zzzz[3] if the position is 4 in the list.

Use this code for java,
Assumptions
a) the locator type is id
b) name of the list is nameOfYourElements

List<WebElement> nameOfYourElements = wd.findElements(By.id("nameOfYourID"));
System.out.println(nameOfYourElements.size());