Ela
Ela

Reputation: 419

Find an element which has multiple div in Selenium

I am trying to find an element which is like a place holder. I will draganddrop other element to this. HTML has

<div class="value1" > <div class="value2" data-id="some_number" data-aura+"value"> 
<div class="place holder"> div2, div3 > Add component </div>

<div class="row contentPanel" data-aura-render="53:56;a"><div class="col-lg-12" data-aura-rendered-by="54:56;a">
<div class="interactions siteforceDesignTimeRegion" data-region-name="content" data-allow-drop="true" data-item-id="87f9712-27f4-564-9445-fg30cf4321a7" data-aura-render="6:56;a" data-aura-class="siteforceDesignTimeRegion">
<div class="Placeholder siteRegion" data-aura-render="10:56;a" data-aura-class="forceDesignTimeEmptyRegion">Add components to this place</div>
<!--render facet: 18:56;a--></div></div></div>

I have tried the below in my code. It actually works in console. But its not working when I used it in code. Would be helpful if someone provides an answer with cssselector or xpath. Thanks.

driver.findElement(By.xpath("//div[contains(@class, 'row contentPanel')]"));

Upvotes: 0

Views: 2865

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23815

From comment :-

it throws this error..org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

There could be following reasons for this exception as below :-

  • May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element present on DOM as below :-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement el = wait.until(ExpectedConditions.presenceOf‌​ElementLocated(By.cs‌​sSelector("div.row.c‌​ontentPanel")));
    
  • May be this element is inside any frame/iframe. If it is, you need to switch that frame/iframe before finding the desired element as below :-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Wait until frame/iframe to available and then switch to it
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
    
    //Now find the desired element 
    WebElement el = wait.until(ExpectedConditions.presenceOf‌​ElementLocated(By.cs‌​sSelector("div.row.c‌​ontentPanel")));
    
    //Once all your stuff done with this frame/iframe need to switch back to default for further stuff
    driver.switchTo().defaultContent();
    

Upvotes: 2

Related Questions