Eliyahu February 2016

Selenium WebDriver. Select element from div list

I have the following area on the HTML page:

<div class="t2-selector">  
    <div class="">  
        USA  
        <div>  
            <div>  
                <div>  
                    <div class="selected" asset-id="129">Google</div>  
                    <div asset-id="130">Microsoft</div>  
                    <div asset-id="126">Apple</div>  
                </div>  
           </div>  
       </div>  
</div>  
<div class="inactive">  
    Europe  
    <div>  
        <div>  
            <div>  
                <div class="inactive" asset-id="127">BT</div>  
            </div>  
        </div>  
   </div>  
</div>  
<div class="">  
    Currencies  
    <div>  
        <div>  
            <div>  
                <div asset-id="135">EUR/USD</div>  
                    <div asset-id="136" class="">GBP/USD</div>  
                    <div asset-id="137" class="">USD/JPY</div>  
                    <div asset-id="138" class="selected">USD/CHF</div>  
                    <div asset-id="139">AUD/USD</div>  
                    <div asset-id="140">USD/CAD</div>  
                </div>  
            </div>  
       </div>  
</div>  

So I need to select desired element from one of the groups (that should not be inactive, OK).
When I'm selecting a group nothing occurs, no error and I don't see the the selected group opens. Even for a moment.
But when I'm trying to select an element in previously clicked group I receive

org.openqa.selenium.ElementNotVisibleException: element not visible  

error.
So I understand that in the moment I'm clicking on desired element it is not visible since the group not appears

Answers


Guy February 2016

You can hover over the drop down to open it and then click on your element

// simulate mouse movement to the dropdown 
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.xpath(xpath1))).perform();

// wait for the element to be visible before clicking on it
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(xpath2)).click();


Shubham Jain February 2016

You can also try to click using JavascriptExecutor

WebElement element= driver.findElement(By.xpath("Your Xpath"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Hope it will help you :)

Post Status

Asked in February 2016
Viewed 3,979 times
Voted 7
Answered 2 times

Search




Leave an answer